您的位置:首页 > 运维架构 > Shell

shell训练营Day20

2019-01-14 13:45 330 查看

练习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

练习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

练习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

练习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

练习45
想办法把文本里面每三行内容合并到一行

#!/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]

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell linux