IF语句小练习

  1. 判断内存小于100,邮件报警

[root@shnne ~]# sh baojing.sh 
the mem is 570,it's ok.
[root@shnne ~]# vim baojing.sh 
#!/bin/sh
memvalue=`free -m|awk 'NR==3 {print $NF}'`
if [ $memvalue -gt 100 ]
then
     echo "the mem is $memvalue,it's ok."
 else
     echo "the mem is $memvalue,less than 100."|mail -s "mem waring" 1138269007@qq.com
fi

2.判断两个数的大小

[root@shnne ~]# cat if1.sh 
#!/bin/sh
#read -p "please input two number:" a b
a=$1
b=$2
[ -z $a ] || [ -z $b ] && {
 echo "USAGE:num1 num2"
 exit
}
expr $a + 0 >/dev/null 2>&1
RETVAL1=$?
expr $b + 0 >/dev/null 2>&1
RETVAL2=$?
if [ $RETVAL1 -ne 0 ] || [ $RETVAL2 -ne 0 ] 
 then  
   echo "please input number"
   exit
fi
if [ $a -lt $b ]
 then
   echo "$a < $b"
elif [ $a -eq $b ]
 then
   echo "$a = $b"
else
   echo "$a > $b"
fi
exit

3.菜单打印

[root@shnne ~]# cat menu.sh 
#!/bin/sh
#echo "*********************************"
#echo "*  Please choice the menu list  *"
#echo "*  1.print the hostname         *"
#echo "*  2.exit                       *"
#echo "*********************************"
menu() {
cat <<END
*********************************
*  Please choice the menu list  *
*  1.print the hostname         *
*  2.exit                       *
*********************************
END
}
menu
read -t 5 -p "Please choice the num:" a
[ $a -eq 1 ] && {
   echo "the hostname is `/bin/hostname`"
}
[ $a -eq 2 ] && {
   exit
}
[ ! $a -eq 1 -o ! $a -eq 2 ] && {
    echo "please input the choice,bye."
    exit
}

4.限定read后输入的个数

[root@shnne ~]# vim read.sh  
#!/bin/sh
read -p "pls input two num:" a b c
if [ -n "$c" ]
 then
  echo "pls input two nmu" 
  exit 2
fi


分享到:
关键词:Linux运维Shell

网友留言(0 条)

发表评论