四、Shell脚本高级编程实战第四部

一、比较两个数的大小

#!/bin/sh
read -p "Pls input two num:" a b
[ -z "$a" ] || [ -z "$b" ] && {
  echo "Pls input twn num agagin."
  exit 1
}
expr $a + 0 &>/dev/null
RETVAL1=$?
echo $RETVAL1
expr $b+0 &>/dev/null
RETVAL2=$?
echo $RETVAL2
test $RETVAL1 -eq 0 -a $RETVAL2 -eq 0 || {
        echo "Pls input two "num" ssagain."
        exit d2
}
[ $a -lt $b ] && {
        echo "$a < $b"
        exit 0
}
[ $a -gt $b ] && {
        echo "$a > $b"
        exit 0
}
[ $a -eq $b ] && {
        echo "$a = $b"
        exit 0
}
二、打印菜单
menu(){
 cat<<END
   1.[ install lamp ]
   2.[ install lnmp ]
   3.[ exit ]
   pls input the num you mant: 
END
}
menu
read -t 15 a
[ $a -eq 1 -o $a -eq 2 -o $a -eq 3 ]&&{
  echo " pls input right mum."
  exit
}
[ $a -eq 1 ]&&{
    echo "installing lamp"
    sleep 3
    echo "lamp is instlled"
   
    exit       
}
[ $a -eq 2 ]&&{
    echo "installing lnmp"
    sleep 3
    echo "lnmp is instlled"
    menu       
}
[ $a -eq 3 ]&&{
   exit
}
三、开发shell脚本实现如果/server/scripts下面存在if3.sh,就输出if3.sh到屏幕上,如果不存在就创建if3.sh脚本
#!/bin/sh
path=/server/scripts
file=if3.sh
if [ ! -d $path ]
  then
    mkdir -p $path
    echo "$path is not exist,already create it"
fi
if [ ! -f $path/$file ]
  then
    touch $path/$file
    echo "$path/$file is not exist,alreate create it"
    exit
fi
ls -l $path/$file
四、  开发脚本判断系统剩余内存大小,低于100M,报警    ,测试报警成功后,加入系统定时任务每3分钟执行一次检查
#!/bin/sh
used_men=`free -m|awk 'NR==3 {print $NF}'`
if [ $used_men -lt 800 ]
 then
   echo "men is not enough,$used_men"
   echo "men is nout enough,$used_men."|mail -s "men warning $(date +%F)" [email protected]
fi
  
 
 
 
              
 
 
 
 

猜你喜欢

转载自www.cnblogs.com/dangjingwei/p/11608243.html