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

一、比较两个数的大小

#!/bin/sh
read -p "Pls input two num:" a b
[ -z "$a" ] || [ -z "$b" ] && {
  echo "Pls input twn num again."
  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" again."
        exit 2
}
[ $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 -ne 1 -o $a -ne 2 -o $a -ne 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)" 825822293@qq.com
fi
  

 五、监控mysql服务是否正常启动,如果未正常启动,就启动mysql服务

#!/bin/sh
port=`netstat -lntup|grep 3306|wc -l`
echo $port
if [ $port -ne 1 ]
 then
    /data/3306/mysql start
else
   echo "Mysql is running"

fi

posted @ 2019-09-29 15:45  小熊尤里  阅读(362)  评论(0编辑  收藏  举报