if条件语句实战单分支结构

单分支结构

语法:

if  [条件]

  then

      指令

fi

if [条件]:then

        指令

fi


if单分支条件中文编程语法:

如果   [你有房]

    那么

        我就嫁给你

果如


提示:分号相当于命令换行,上面两种语法等用。

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

if [ -f  "$file1" ] :then

    echo 1

fi


范例1:

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
#功能:单分支if结构整数比较,用-lt格式例子
if  [ 10 -lt 12 ]
then 
echo  "yes"
fi
 
执行结果:
[root@XCN  if ] # sh test1.sh 
yes
#提示:当比较条件为整数数字时


范例2:使用read及脚本传参方式如果实现上述整数的比较?

解答:

特别强调:read读入和命令行传参是两种输入内容的方法。

1)脚本传参的方式脚本例子

1
2
3
4
5
6
7
8
#!/bin/bash
if  [ $1 -lt $2 ]:  then
   echo  "yes,$1 less then $2"
fi
 
输入结果
[root@XCN  if ] # sh if2.sh 1 2
yes ,1  less  then  2

2)单分支if判断两整数大小三种情况的脚本例子(以read读入为例)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash
read  -p  "pls input two num:"  a b
if  [ $a -lt $b ]; then
   echo  "yes,$a less than $b"
   exit
fi
if  [ $a - eq  $b ]; then
   echo  "yes,$a eaual $b"
   exit
fi
if  [ $a -gt $b ]; then
   echo  "yes,$a greater than $b"
   exit
fi
 
 
见证奇迹的时刻:
[root@localhost shell] # sh if1.sh 
pls input two num:1 2
yes ,1  less  than 2


范例3:开发shell脚本实现如果/server/scipts下面存在if3.sh的内容就输出到屏幕

注意:如果执行脚本后发现该if3.sh不存在,就自动创建这个if3.sh脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/bin/bash
path= /server/scripts
file =if3.sh
#no1
if  [ ! -d $path ]
then
   mkdir  -p $path
   echo  "$path is not exist,already created it."
fi
#no2
if  [ ! -f $path/$ file  ]
then
   touch  $path/$ file
   echo  "$file is not exist,alreadt created it."
   exit
fi
#no3
echo  "ls -l $path/$file"
ls  -l $path/$ file                                                                         
~                                                                           
执行输出:
                                                                                            
[root@localhost ~] # sh if.sh 
/server/scripts  is not exist,already created it.
if3.sh is not exist,alreadt created it.


范例4:开发脚本判断系统剩余内存大小,如果低于100M就邮件报警。

测试报警成功后加入系统定时任务每3分钟执行一次检查。


思路:

  1. 如果去内容,去内存那个选项。

1
2
[root@ailuoli ~] # free -m|grep buffers/ |awk ' {print $NF}'
1781

  2.发邮件mail,mutt。sendmail服务器要开启

1
2
3
4
5
6
[root@ailuoli ~] # yum install sendmail
[root@ailuoli ~] # /etc/init.d/sendmail start
Starting sendmail: [  OK  ]
Starting sm-client: [  OK  ]
 
[root@ailuoli ~] #echo "xcn"|mail -s "title" 995345781@qq.com

3.编写脚本

1
2
3
4
5
6
#!/bin/bash
userd_mem=` free  -m| grep  buffers/ | awk  ' {print $NF}' `
if  [ $userd_mem -lt 100 ]
then
   echo  "menm is not enough,$userd_men." |mail -s  "mem warning $(date +%F)"  995345781@qq.com
fi

wKiom1lQxDKzLGYzAABrA331L8U422.png-wh_50

三分钟监测一次:

*/3 * * * * /bin/bash /mem.sh




本文转自 baishuchao 51CTO博客,原文链接:http://blog.51cto.com/baishuchao/1941442
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值