总结
方法
01、单分支结构:
if <条件表达式> then 指令 if 或 if <条件表达式>;then 指令 if
02、嵌套:
if <条件表达式>
then
if <条件表达式>
then
指令
if
if
03、双分支结构:
if <条件表达式>;then 指令 else 指令 if
04、多分支结构:
if <条件表达式>;then 指令 elif <条件表达式>;then 指令 else 指令 if
05、监控MySQL数据库:
if [ `netstat -lntup|grep mysqld|wc -l` -gt 0 ];then echo "MySQL is Running." else echo "MySQL is Stopped." /etc/init.d/mysqld start fi
06、监控Nginx Web服务:
if [ `netstat -lntup|grep nginx|wc -l` -gt 0 ];then echo "Nginx is Running." else echo "Nginx is Stopped." /etc/init.d/nginx start fi
07、判断字符串是否为数字:[ -n "`echo oldboy123|sed 's/[]0-9//g'`" ]&&echo char||echo int
08、[ -z "`echo oldboy123|sed 's/[]0-9//g'`" ]&&echo int||echo char
09、num=521;[ -n "$num" -a "$num" = "${num//[^0-9]/}" ]&&echo "num"
10、[[ oldboy123 =~ ^[0-9]+$ ]]&&echo int||echo char
11、监控memcached服务:
#!/bin/sh printf "del key\r\n"|nc 127.0.0.1 11211 &>/dev/null printf "set key 0 0 10 \r\noldboy1234\r\n"|nc 127.0.0.1 11211 &>/dev/null McValues=`printf "get key\r\n"|nc 127.0.0.1 11211|grep oldboy1234|wc -l` if [ $McValues -eq 1 ];then echo "memcached status is ok" else echo "memcached status is bad" fi
12、rsync服务的启动脚本:cat /etc/init.d/rsyncd
#!/bin/bash
if [ $# -ne 1 ];then
echo $"usage:$0 {start|stop|restart}"
exit 1
fi
if [ "$1" = "start" ];then
rsync --daemon
sleep 2
if [ `netstat -lntup|grep rsync|wc -l` -ge 1 ];then
echo "rsyncd is started."
exit 0
fi
elif [ "$1" = "stop" ];then
killall rsync &>/dev/null
sleep 2
if [ `netstat -lntup|grep rsync|wc -l` -eq 0 ];then
echo "rsyncd is stopped."
exit 0
fi
elif [ "$1" = "restart" ];then
killall rsync
sleep 1
killpro=`netstat -lntup|grep rsync|wc -l`
rsync --daemon
sleep 1
startpro=`netstat -lntup|grep rsync|wc -l`
if [ $killpro -eq 0 -a $startpro -ge 1 ];then
echo "rsyncd is restarted."
exit 0
fi
else
echo $"usage:$0 {start|stop|restart}"
exit 1
fi
13、rsync的启动脚本:cat /etc/init.d/rsyncd
#!/bin/bash
# chkconfig: 2345 20 80
# description: Rsyncd Startup scripts by oldboy.
if [ $# -ne 1 ];then
echo $"usage:$0 {start|stop|restart}"
exit 1
fi
if [ "$1" = "start" ];then
rsync --daemon
sleep 2
if [ `netstat -lntup|grep rsync|wc -l` -ge 1 ];then
echo "rsyncd is started."
exit 0
fi
elif [ "$1" = "stop" ];then
killall rsync &>/dev/null
sleep 2
if [ `netstat -lntup|grep rsync|wc -l` -eq 0 ];then
echo "rsyncd is stopped."
exit 0
fi
elif [ "$1" = "restart" ];then
killall rsync
sleep 1
killpro=`netstat -lntup|grep rsync|wc -l`
rsync --daemon
sleep 1
startpro=`netstat -lntup|grep rsync|wc -l`
if [ $killpro -eq 0 -a $startpro -ge 1 ];then
echo "rsyncd is restarted."
exit 0
fi
else
echo $"usage:$0 {start|stop|restart}"
exit 1
fi
----------
chkconfig --list rsyncd
chkconfig --add rsyncd
chkconfig --list rsyncd
浙公网安备 33010602011771号