现在的位置: 首页 > 综合 > 正文

shell习题100(九)

2020年02月12日 综合 ⁄ 共 1298字 ⁄ 字号 评论关闭

题目要求

编写一个问候程序,它执行时能根据系统当前的时间向用户输出问候信息。假设从半夜到中午为早晨,中午到下午六点为下午,下午六点到半夜为晚上。

参考答案

#!/bin/bashd=`date +%H`if [ $d -ge 0 -a $d -lt 7 ]then tag=1elif [ $d -ge 7 -a $d -lt 12 ]then tag=2elif [ $d -ge 12 -a $d -lt 18 ]then tag=3else tag=4ficase $tag in 1)echo "早晨好" ;; 2)echo "上午好";; 3)echo "下午好";; 4)echo "晚上好";; *)echo "脚本出错啦";;esac

题目要求

写一个shell脚本,实现简单的弹出式菜单功能,用户能根据显示的菜单项从键盘选择执行对应的命令。

参考答案

#!/bin/bashPS3="Please input your choice(1-4): "select i in w ls pwd quitdo case $i inw) w ;;ls) ls ;;pwd) pwd ;;quit) exit ;;*) echo "Please input 1-3." ;; esacdone

参考答案2

#!/bin/bashecho -e "1) wn2) lsn3) pwdn4) quit"while :doread -p "Please input your choice(1-4): " ccase $c in 1)w;; 2)ls;; 3)pwd;; 4)exit;; *)echo "Please input 1-4.";;esacdone

题目要求

写一个shell脚本,执行中每隔5分钟检查指定的用户是否登录系统,用户名从命令行输入,如果指定的用户已经登录,则显示相关信息。

参考答案

#!/bin/bashwhile :do if w|sed '1'd|awk '{print $1}'|grep -qw "$1" thenecho "用户$1 已经登录系统."exit fi sleep 300done

题目要求

先普及一个小常识,我们用ps aux可以查看到进程的PID,而每个PID都会在/proc内产生。如果查看到的pid在proc内是没有的,则进程被人修改了,这就代表系统很有可能已经被入侵过了。请用上面知识编写一个shell,定期检查下自己的系统是否被人入侵过

参考答案

#!/bin/bashpp=$$ps -elf |sed '1'd > /tmp/pid.txtfor pid in `awk -v ppn=$pp '$5!=ppn {print $4}' /tmp/pid.txt`do if ! [ -d /proc/$pid ] thenecho "系统中并没有pid为$pid的目录,需要检查。" fi done

题目要求

想办法把文本里面每三行内容合并到一行例如:1.txt内容

1234567

处理后应该是

1 2 34 5 67

参考答案

#!/bin/bashn=1cat $1 |while read linedo n1=$[$n%3] if [ $n1 -eq 0 ] thenecho "$line" elseecho -n "$line " fi n=$[$n+1]done

以上就上有关shell习题100(九)的全部内容,学步园全面介绍编程技术、操作系统、数据库、web前端技术等内容。

抱歉!评论已关闭.