转载自:
http://www.apelearn.com/
https://coding.net/u/aminglinux/p/shell100/git
自己动手验证每一个Shell习题
Shell 100-41
####题目要求 编写一个问候程序,它执行时能根据系统当前的时间向用户输出问候信息。假设从半夜到中午为早晨,中午到下午六点为下午,下午六点到半夜为晚上。 ####参考答案 #!/bin/bash d=`date +%H` if [ $d -ge 0 -a $d -lt 7 ] then tag=1 elif [ $d -ge 7 -a $d -lt 12 ] then tag=2 elif [ $d -ge 12 -a $d -lt 18 ] then tag=3 else tag=4 fi case $tag in 1) echo "早晨好" ;; 2) echo "上午好" ;; 3) echo "下午好" ;; 4) echo "晚上好" ;; *) echo "脚本出错啦" ;; esac
Shell 100-42
####题目要求 写一个shell脚本,实现简单的弹出式菜单功能,用户能根据显示的菜单项从键盘选择执行对应的命令。 ####参考答案 #!/bin/bash PS3="Please input your choice(1-4): " select i in w ls pwd quit do case $i in w) w ;; ls) ls ;; pwd) pwd ;; quit) exit ;; *) echo "Please input 1-3." ;; esac done ####参考答案2 #!/bin/bash echo -e "1) w\n2) ls\n3) pwd\n4) quit" while : do read -p "Please input your choice(1-4): " c case $c in 1) w ;; 2) ls ;; 3) pwd ;; 4) exit ;; *) echo "Please input 1-4." ;; esac done
Shell 100-43
####题目要求 写一个shell脚本,执行中每隔5分钟检查指定的用户是否登录系统,用户名从命令行输入,如果指定的用户已经登录,则显示相关信息。 ####参考答案 #!/bin/bash while : do if w|sed '1'd|awk '{print $1}'|grep -qw "$1" then echo "用户$1 已经登录系统." exit fi sleep 300 done
Shell 100-44
####题目要求 先普及一个小常识,我们用ps aux可以查看到进程的PID,而每个PID都会在/proc内产生。如果查看到的pid在proc内是没有的,则进程被人修改了,这就代表系统很有可能已经被入侵过了。 请用上面知识编写一个shell,定期检查下自己的系统是否被人入侵过 ####参考答案 #!/bin/bash pp=$$ ps -elf |sed '1'd > /tmp/pid.txt for pid in `awk -v ppn=$pp '$5!=ppn {print $4}' /tmp/pid.txt` do if ! [ -d /proc/$pid ] then echo "系统中并没有pid为$pid的目录,需要检查。" fi done
Shell 100-45
####题目要求 想办法把文本里面每三行内容合并到一行 例如:1.txt内容 1 2 3 4 5 6 7 处理后应该是 1 2 3 4 5 6 7 ####参考答案 #!/bin/bash n=1 cat $1 |while read line do n1=$[$n%3] if [ $n1 -eq 0 ] then echo "$line" else echo -n "$line " fi n=$[$n+1] done