shell入门篇之流程控制——条件判断
题目:
1.判断当前主机的cpu生产商,其信息在/proc/cpuinfo文件中vendor_id一行中
2.根据用户输入成绩,判断优良中差(A,B,C,D, 注意边界问题)
3.判断 sshd 进程是否运行,如果服务启动打印启动,未启动则打印未启动(使用查看进程和端口两种方式)
4.检查主机是否存活,并输出结果(使用for循环实现:主机数>2)
一,if条件语句的语法及案例
1,单分支结构
第一种语法:
if <条件表达式>
then
指令
fi
第二种语法:
if <条件表达式>;then
指令
fi
2,双分支结构
if <条件表达式>
then
指令序列1
else
指令序列2
fi
3,多分支结构
语法:
if 条件表达式1
then
命令序列1
elif 条件表达式2
then
命令序列2
elif 条件表达式3
then
命令序列3
else
命令序列n
fi
在上面的语法中,当整个if elif语句结构中的第1个条件表达式为真,则执行第1个then子句中的语句 statement1;否则,继续下面的判断。如果条件表达式2的值为真,则执行第2个then子句中的语句,以 此类推。如果所有的条件表达式的值都为假,则执行最后的else子句中的语句。最后是if elif结构的结束 标志fi。
示例1
1,判断当前主机的cpu生产商,其信息在/proc/cpuinfo文件中vendor_id一行中
result=`cat /proc/cpuinfo | grep "vendor_id" | uniq | cut -d ":" -f 2`
if [[ $result =~ [[:space:]]*GenuineIntel$ ]];then
echo " cup is GenuineIntel "
elif [[ $result =~ [[:space:]]*AuthenticAMD$ ]];then
echo " cup is AuthenticAMD "
else
echo " unkNown "
fi
###########################################
结果
[root@server day03]# sh if_test_cpu.sh
cup is GenuineIntel
示例2
2.根据用户输入成绩,判断优良中差( 注意边界问题)
read -p "please ipput your grades(0-150):" score
if test -z "$score";then
echo "you should input your grades"
exit 1
fi
expr $score + 1 &> /dev/null
return_num=$?
if ! [ "$return_num" -eq 0 ];then
echo "please input number"
exit 2
fi
if [[ "$score" -gt 150 ]];then
echo "please check your grades and input a number bewteen 0 and 150"
elif test "$score" -lt 0 ;then
echo "please check your grades and input a number bewteen 0 and 150"
elif [ "$score" -ge 130 ];then
echo " excellent "
elif [ "$score" -ge 110 ];then
echo " good "
elif [ "$score" -ge 96 ];then
echo " qualified "
else
echo " fasle "
fi
#####################################
结果
please ipput your grades(0-150):-10
please check your grades and input a number bewteen 0 and 150
[root@server day03]# sh if_script.sh
please ipput your grades(0-150):10
fasle
[root@server day03]# sh if_script.sh
please ipput your grades(0-150):100
qualified
[root@server day03]# sh if_script.sh
please ipput your grades(0-150):130
excellent
[root@server day03]# sh if_script.sh
please ipput your grades(0-150):120
good
[root@server day03]# sh if_script.sh
please ipput your grades(0-150):200
please check your grades and input a number bewteen 0 and 150
示例3
3.判断 sshd 进程是否运行,如果服务启动打印启动,未启动则打印未启动(使用查看进程和端口两种方式)
echo "View the process"
result1=`ps -ef | grep sshd | grep -v grep | wc -l`
result2=`netstat -lntup | grep -w 22 | wc -l`
if [ $result1 -ge 1 ];then
echo sshd is running
else
echo sshd is not running
fi
echo "View the port"
if [ $result2 -ge 1 ];then
echo sshd is running
else
echo sshd is not running
fi
####################################
结果:
[root@server day03]# sh if_test_ssh.sh
View the process
sshd is running
View the port
sshd is running
示例4
4.检查主机是否存活,并输出结果(使用for循环实现:主机数>=2)
for i in {8..9}
do
ping -c 4 -W 1 192.168.240.12"$i" &> /dev/null
// 检测时 echo $? 会导致最后都是running
if [ $? -eq 0 ];then
echo " 192.168.240.12$i is running "
else
echo " 192.168.240.12$i is not running "
fi
done
############################
结果:
[root@server day03]# sh for_test_ping.sh
192.168.240.128 is running
192.168.240.129 is not running
示例5
5.编写脚本,判断当前系统剩余内存大小,如果低于100M,邮件报警管理员,使用计划任务,每10分钟检查一次。
rpm -q sendmail &> /dev/null
result1=$?
if [ $result1 -ne 0 ] ;then
yum install -y sendmail &> /dev/null
echo " download sendmail "
else
echo " sendmail alreay installed "
fi
rpm -q mailx &> /dev/null
result2=$?
if [ $result2 -ne 0 ] ;then
yum install -y mailx &> /dev/null
echo " download mailx "
else
echo " mailx already"
fi
free=`free -m | grep Mem | tr -s " " | cut -d " " -f 4`
echo $free
if [ "$free" -le 10000 ] ;then
echo " 剩余内存:${free},低于100M" | mail -s "内存报警" student@node1
fi
echo " mailx already"
fi
free=`free -m | grep Mem | tr -s " " | cut -d " " -f 4`
echo $free
if [ "$free" -le 10000 ] ;then
echo " 剩余内存:${free},低于100M" | mail -s "内存报警" student@node1
fi