blog

Bash Shell编程指南之if判断

单分支结构语法

if [ 条件 ]
then
  指令
fi

或者

if [ 条件 ];then
	指令
fi

分号相当于命令换行,上面两种语法等同

特殊写法:if[ -f “$file1” ];then echo 1;fi相当于:[ -f “$file1” ] && echo 1

if [ -f "$file1" ];then
	echo 1
fi

单分支范例

if比对两个文件的大小

#!/bin/bash
a=$1
b=$2
if [ -z "$a" -o -z "$b" ];then 
 echo "Error: Please Output integer." 
 exit 1
fi

expr "$a" + 1  >/dev/null 2>&1

if [ "$?" -ne 0 ];then
  echo "Error: a must be an integer."
  exit 1
fi

expr "$b" + 1 >/dev/null 2>&1

if [ "$?" -ne 0 ];then
  echo "Error: b must be an integer."
  exit 1
fi

[ "$a" -gt "$b" ] && echo "$a > $b"
[ "$a" -lt "$b" ] && echo "$a < $b"
[ "$a" -eq "$b" ] && echo "$a = $b"
# 使用read传参
$ cat if_read.sh 
#!/bin/bash
read -p "请输入两个整数:" a b

if [ -z "$a" -o -z "$b" ];then 
 echo "Error: Please Output integer." 
 exit 1
fi

expr "$a" + 1  >/dev/null 2>&1

if [ "$?" -ne 0 ];then
  echo "Error: a must be an integer."
  exit 1
fi

expr "$b" + 1 >/dev/null 2>&1

if [ "$?" -ne 0 ];then
  echo "Error: b must be an integer."
  exit 1
fi

[ "$a" -gt "$b" ]&&echo "$a > $b"
[ "$a" -lt "$b" ]&&echo "$a < $b"
[ "$a" -eq "$b" ]&&echo "$a = $b"

当内存低于100M的时候发邮件报警,定时任务每三分钟过执行一次

$ cat if_dan.sh 
#!/bin/bash
RAM=$(free -m | sed -n 3p | awk '{print $4}')

if [ "$RAM" -lt 100 ];then
  echo "The remaining memory space is less than 100M"|mail -s 'Caveat' root@shell
  exit 0
fi
$ tail -1 /var/spool/cron/root 
*/3 * * * * /bin/sh /server/scripts/02/if_dan.sh >/dev/null 2>&1

内存、磁盘、NFS、MySQL、WEB这些都可以做监控。

if双分支结构语法

语法结构

if [ 条件 ]
then
  指令集
else
  指令集
fi

特殊写法:if [ -f "$file1" ];then echo 1;else echo 0;fi 相当于:[ -f "$file1" ] && echo 1 || echo 0

if多分支结构语法

  1. 注意多分支elif的写法elif [条件];then,不要落下了then
  2. 结尾的else后面没有then

多分支范例

使用read传参的方式进行判断比较

#!/bin/bash
read -p "请输入两个整数:" a b

if [ -z "$a" -o -z "$b" ];then 
 echo "Error: Please Output integer." 
 exit 1
fi

expr "$a" + 1  >/dev/null 2>&1
if [ "$?" -ne 0 ];then
  echo "Error: a must be an integer."
  exit 1
fi

expr "$b" + 1 >/dev/null 2>&1
if [ "$?" -ne 0 ];then
  echo "Error: b must be an integer."
  exit 1
fi

if [ "$a" -gt "$b" ];then
  echo "$a > $b"
  exit 0
elif [ "$a" -lt "$b" ];then
  echo "$a < $b"
  exit 0
else
  echo "$a = $b"
  exit 0
fi

开发脚本实现如果/server/scripts/下面存在if3.sh就输出屏幕,如果不存在就自动创建if3.sh这个脚本

$ cat if-file.sh 
#!/bin/bash
FILEPATH=/server/scripts/if3.sh
FILENAME=$(ls $FILEPATH 2>/dev/null)

if [ "$?" -ne 0 ];then
  touch /server/scripts/if3.sh
  echo "create file ok."
  exit
fi

if [ "$?" -eq 0 ];then
  echo "$(cat $FILEPATH)"
  exit
fi

判断字符串是否为数字的多种方法

$ [ $(echo 1 | sed 's/[0-9]//g') -a $(echo 2 | sed 's/[0-9]//g') ] && echo '参数正确!' || echo '参数必须是整数!'
$ n=as1
$ [ -z $(echo "${n//[0-9]/}") ] && echo 1 || echo 0
0
$ n=1as1
$ [ -z $(echo "${n//[0-9]/}") ] && echo 1 || echo 0
0 # 前面的结果不为0,有非数字字符
$ n=111
$ [ -z $(echo "${n//[0-9]/}") ] && echo 1 || echo 0
1 # 前面没有非数字字符
$ [ -n $num -a $num = ${num//[0-9]/} ] && echo 0 || echo 1
1
$ num=a
$ [ -n $num -a $num = ${num//[0-9]/} ] && echo 0 || echo 1
0
expr $1 + 0 >/dev/null 2>&1
[ $? -eq 0 ] && echo 1
$ [[ a =~ [0-9] ]] && echo int || echo char
char
$ [[ 1 =~ [0-9] ]] && echo int || echo char
int
[[ $1 =~ ^[0-9]+$ ]] &&echo int ||echo char

扩展:判断字符串长度是不是为0的多种思路

[ -z "as" ] && echo 1 || echo 0
${#arg} -eq 0
expr length "as"
[ `expr length "as"` -eq 0 ]
echo "as" | wc -L

WEB服务监控手段

  1. 本地:ss,netstat,lsof
  2. 远程:telnet,nmap,nc
$ ps -ef | grep apache | wc -l
9
$ curl -I -s 172.16.1.10|head -1
HTTP/1.1 403 Forbidden
$ curl -I 172.16.1.10 2>/dev/null|head -1
HTTP/1.1 403 Forbidden
$ curl -I -s -w "%{http_code}" -o /dev/null 172.16.1.10
403
$
$ wget --spider --timeout=100 172.16.1.10
$ wget --spider --timeout=100 --tries=2 172.16.1.101
# 超时时间,重试次数
$ echo $?
# 看返回值
$ lsof -i :80|wc -l
10
$ nmap 172.16.1.10 -p 80 | grep open | wc -l
1
$ telnet 172.16.1.10 80
Trying 172.16.1.10...
Connected to 172.16.1.10.
Escape character is '^]'.