shell 练习题

1.编写脚本/bin/per.sh,判断当前用户对指定参数文件,是否不可读并且不可写

read -p "Please Input A File: " file
if [ ! -e $file  ];then
        echo "$file not exits"
elif [ -f $file ];then
        if [ ! -r $file -a ! -w $file  ];then
                echo "User Not read and write"
        else
                echo "$file can read or write"
        fi
else
        echo "$file is not a normal file"
fi

2.编写脚本/root/bin/excute.sh,判断参数文件是否为sh后缀的普通文件,如果是,添加所有人可执行权限,否则提示用户非脚本文件

read -p "Input Shell File: " file
if [ -f $file ];then
	suffix=`echo "$file" | awk -F. '{print $NF}'`
	if [ $suffix == "sh" ];then
		chmod +x $file 
		echo "$file add chmod X success";
	else
		echo "$file is not .sh file"
	fi
else
	echo "file is not a normal file"
fi

3.编写脚本/root/bin/nologin.sh 和login.sh,实现禁止和允许普通用户登录系统

login.sh

[ -f "/etc/nologin" ] && rm -rf /etc/nologin && echo "User Can Login" ||echo "User Already Login"

nologin.sh

[ -f "/etc/nologin"  ] && echo "Other User Cannot Login System" ||{ touch /etc/nologin; echo "Other User Cannot Login System"; }

4.编写脚本/root/bin/sumid.sh,计算/etc/passwd文件中的第10个用户和第20个用户的ID之和

sum =`awk -F: 'BEGIN{sum=0} NR==10||NR==20 { sum+=$3 } END{ print sum }' /etc/passwd`
echo $sum

5.用两种以上的方式,查目的地当地服务器上面io最繁忙的是哪块硬盘

1.用top命令查看,键入top后显示菜单
wa为等待IO时间,观察这个选项,如果 wa的数量比较大,说明等待输入输出的的io比较多。

2.使用iostat,可以非常详细的查看到是哪块硬盘最繁忙,iostate 默认不安装在系统中,需要使用Yum安装: yum install iostat -y

iostat -x 可以显示详细的Io信息,其中
%util 表示磁盘忙碌情况,一般该值超过80%表示该磁盘可能处于繁忙状态。

iostat -x | awk '$NF ~ /[0-9]+/ && NR>6  {disk[$1]=$NF} END{for(i in disk) print i,disk[i] }'|sort -rn -k2|head -n1

使用命令截取后,可以取到最高值

6.在12月内,每天早上6点到12点,每隔3个小时0分钟执行一次/usr/bin/backup

0 6-12 * 12 * /usr/bin/backup
原文地址:https://www.cnblogs.com/ddz-linux/p/10473336.html