分支与循环结构
if语句是实际生产工做中最重要且最经常使用的语句,因此,必须掌握牢固。 mysql
if条件句
if条件句语法
语法 nginx
-
if [ 条件 ]
-
then
-
指令
-
fi
-
或
-
if [ 条件 ];then
-
指令
-
fi
条件表达式[ -f "$file1" ]&& echo 1,至关于下面的if语句。 web
-
if [ -f "$file1" ];then
-
echo 1
-
fi
-
if [ 条件 ]
-
then
-
指令集1
-
else
-
指令集2
-
fi
条件表达式[ -f "$file1" ]&& echo 1||echo 0,至关于双分支if [ -f "$file1" ];then echo 1;else echo 0;fi。 sql
-
if 条件
-
then
-
指令
-
elif 条件
-
then
-
指令
-
elif 条件
-
then
-
指令
-
...
-
else
-
指令
-
fi
单分支if条件句
开发shell脚本判断系统剩余内存的大小,若是低于100M就邮件报警给管理员,并加入系统定时任务每三分钟执行一次检查。 shell
free -m|awk 'NR==2{print $4}' 数据库
-
[root@lamp ~]# cat free_m.sh
-
#!/bin/bash
-
-
FREE=`free -m|awk 'NR==3{print $4}'`
-
if [ $FREE -lt 100 ]
-
then
-
echo "warning:The available memory $FREE."
-
exit 0
-
fi
-
echo "The available memory $FREE."
双多分支if条件句
用if双分支实现read读入的方式比较两个数的大小。 bash
-
[root@lamp ~]# cat c3.sh
-
#!/bin/bash
-
read -p "Pls input two nums: " num01 num02
-
[ -z $num01 ]&&{
-
echo "the num01 you input must be int."
-
exit 2
-
}
-
[ -z $num02 ]&&{
-
echo "the num02 you input must be int."
-
exit 2
-
}
-
expr $num01 + $num02 + 1 &>/dev/null
-
[ $? -ne 0 ]&&{
-
echo "the num you input must be int."
-
exit 2
-
}
-
if [ $num01 -lt $num02 ]
-
then
-
echo "$num01 < $num02."
-
elif [ $num01 -gt $num02 ]
-
then
-
echo "$num01 > $num02."
-
else
-
echo "$num01 = $num02."
-
fi
用if双分支实现对nginx或mysql服务是否正常进行判断,使用进程数、端口、url的方式判断,若是进程没起,把进程启动。 curl
web服务和数据库(mysql)的监控方法。 tcp
一、端口监控 this
本地监控:netstat、ss、lsof
远程监控:telnet、nmap、nc
telnet监控端口
-
[root@lamp ~]# echo -e "\n"|telnet www.baidu.com 80|grep Connected|wc -l
-
Connection closed by foreign host.
-
1
nmap监控端口
-
[root@lamp ~]# nmap www.baidu.com -p 80|grep open|wc -l
-
1
nc监控端口
-
[root@lamp ~]# nc -z 192.168.163.128 22|grep succeeded|wc -l
-
1
二、进程监控
本地监控:ps -ef|grep mysql|wc -l
三、wget、curl,http方式根据返回值或者返回内容判断。
四、header(http),http方式根据状态码判断。
五、数据库特有经过mysql客户端链接,根据返回值或者返回内容判断。
-
[root@lamp ~]# cat check_db.sh
-
#!/bin/bash
-
#local
-
if [ "`netstat -lnt|grep 3306|awk -F "[ :]+" '{print $5}'`" = "3306" ]
-
#if [ `ps -ef|grep mysql|grep -v grep|wc -l` -gt 0 ]
-
#if [ `netstat -lntup|grep mysqld|wc -l` -gt 0 ]
-
#if [ `lsof -i tcp:3306|wc -l` -gt 0 ]
-
-
#remote
-
#if [ `nmap 192.168.1.123 -p 3306 2>/dev/null|grep open|wc -l` -gt 0 ]
-
#if [ `nc -w 2 192.168.1.123 3306 &>/dev/null&&echo ok|grep ok|wc -l` -gt 0 ]
-
then
-
echo "Mysql is Running."
-
else
-
echo "Mysql is Stopped."
-
/deta/mysql start
-
fi
-
[root@lamp ~]# cat check_web.sh
-
#!/bin/bash
-
if [ "`curl -I -s -o /dev/null -w "%{http_code}\n" http://192.168.1.123`" = "200" ]
-
#if [ `curl -I http://192.168.1.123 2>/dev/null|head -1|egrep "200|302|301"|wc -l` -eq 1 ]
-
#curl -s http://192.168.1.123 &>/dev/null
-
#if [ $? -eq 0 ]
-
#if [ "`curl -s http://192.168.1.123 &>/dev/null&&echo $?`" = "0" ]
-
#if [ "`curl -s http://192.168.1.123`" = "bbs" ]
-
then
-
echo "httpd is running."
-
else
-
echo "httpd is stopped."
-
fi
经过传参的方式往/etc/user.conf里添加用户,具体要求以下:
一、命令用法:USAGE:sh adduser {-add|-del|-search} username
二、传参要求:若是参数为-add,表示添加后面接的用户名;若是参数为-del,表示删除后面接的用户名;若是参数为-search,表示查找后面接的用户名。
三、若是有同名的用户则不能添加,没有对应用户则无需删除,查找到用户以及没有用户时给出明确提示。
四、/etc/user.conf不能被全部外部用户之间删除或修改。
-
[root@lamp ~]# cat user.sh
-
#!/bin/bash
-
ROOT_UID=0
-
-
if [ "$UID" -ne "$ROOT_UID" ]
-
then
-
echo "Mast be root to run this script."
-
exit 1
-
fi
-
if [ $# -ne 2 ]
-
then
-
echo "USAGE:sh $0 {-add|-del|-search} username."
-
exit 2
-
fi
-
check=$1
-
name=$2
-
if [ "$check" = "add" ]
-
then
-
result=`cat /etc/user.conf|grep -Fx "$name"`
-
[ -z $result ]&&{
-
echo "$name" >> /etc/user.conf
-
echo "user add "$name" is ok."
-
exit 0
-
}
-
echo "user $name is in."
-
exit 0
-
elif [ "$check" = "del" ]
-
then
-
result=`cat /etc/user.conf|grep -Fx "$name"`
-
[ -z $result ]&&{
-
echo "user "$name" not find."
-
exit 0
-
}
-
sed -ri /^$name$/d /etc/user.conf
-
echo "user del "$name" is ok."
-
elif [ "$check" = "search" ]
-
then
-
result=`cat /etc/user.conf|grep -Fx "$name"`
-
[ -z $result ]&&{
-
echo "user $name not find."
-
exit 0
-
}
-
echo $result
-
exit 0
-
else
-
echo "USAGE:sh $0 {-add|-del|-search} username."
-
exit 1
-
fi
获取文件md5值,防篡改。
-
[root@lamp ~]# find ./ -type f|xargs md5sum
将md5值写入文件。
-
[root@lamp ~]# find ./ -type f|xargs md5sum >/tmp/md5list
比较md5值。
-
[root@lamp ~]# md5list -c /tmp/md5list