一、if条件语句

  1、语法

    1)单分支结构

第一种
if <条件表达式>
then
指令
fi 第二种
if <条件表达式>;then
指令
fi 上文的"<条件表达式>"部分可以时test、[]、[[]]、(())等条件表达式,甚至可以直接使用命令作为条件表达式。每个if语句都以if开头,并带有then,最后以fi结束
第二种语法中分号相当于命令换行,含义医院 当if后面的<条件表达式>成真时,就会执行then后面的指令或语句;否则,就会忽略then后面的指令或语句,转而执行fi下面的程序
条件语句还可以嵌套(就是if语句里面还有if条件语句)如下面语法示例:
if <条件表达式>
  then
    if <条件表达式>
      then
        指令
    fi
fi

    2)双分支结构

      复习一下单分支

        如果...,那么...

      双分支

        如果...,那么...,否则...

      结构如下:

if <条件表达式>
then
指令集1
else
指令集2
fi 测试条件表达式[ -f "$file1" ]&&echo || echo 0相当于下面:
if [ -f "$file1" ]
then
echo
else
echo
fi

    3)多分支结构

      结构主体

        如果...,那么...,否则如果...,那么...,否则如果...,那么...,否则...

      结构如下:

if <条件表达式1>
then
指令1
elif <条件表达式2>
then
指令2
else
指令3
fi
注意:每个elif都必须带then,else没有then

    4)条件表达式 test、[]、[[]]、(())等条件表达式语法

      1.4.1、test条件表达式

if test
then
指令
fi

      1.4.2、[]条件表达式

if [ 字符串或算术表达式 ]
then
指令
fi

      1.4.3、[[]]条件表达式

if [[ 字符串表达式 ]]
then
指令
fi

      1.4.4、(())条件表达式

if (( 算术表达式 ))
then
指令
fi

      1.4.5、命令表达式

if  命令
then
指令
fi

      

二、if条件语句示例

  1、单分支示例

    1)把下面测试文件中表达式的语句改成if条件语句

[root@web1 scripts]# [ -f /etc/hosts ]&& echo 1
1

[root@web1 scripts]# [[ -f /etc/hosts ]]&& echo 1
 1
 [root@web1 scripts]# test -f /etc/hosts && echo 1
 1

[root@web1 scripts]# chmod +x test15
chmod: cannot access ‘test15’: No such file or directory
[root@web1 scripts]# chmod +x test15.sh
[root@web1 scripts]# cat test15.sh
#!/bin/bash
if [ -f /etc/hosts ]
then
echo "[1]"
fi if [[ -f /etc/hosts ]]
then
echo "[1]"
fi if test -f /etc/hosts
then
echo "test1"
[root@web1 scripts]# ./test15.sh
[]
[]
test1

    2)开发shell脚本判断系统剩余内存的大小,如果低于3850mb,就邮件报警给系统管理员,并且讲脚本加入系统定时任务,每3分钟执行一次

    3步法则:

      1)分析需求

      2)设计思路

          获取系统剩余内存值-命令

          配置邮件报警-可用第三方邮件服务器

          判断取值是否小于100mb,如小于就报警-if

          编码实现shell脚本

          加入crond定时任务,每三分钟检查一次

      3)编码实现

         1.取内存值

[root@web1 ~]# free -m
total used free shared buff/cache available
Mem:
Swap: 3840 #<---这里取3840这个值作为可用内存
[root@web1 ~]# free -m |awk 'NR==3{print $NF}' #<---利用awk获取到3840单位时MB [root@web1 ~]#

        2.邮件设置

[root@web1 ~]# echo -e "set from=zhutoyearn@163.com smtp=smtp.163.com\nset smtp-auth-user=zhutoyearn smtp-auth-password=1qazxsw2 smtp-auth=login" >>/etc/mail.rc
[root@web1 ~]# !tail
tail - /etc/mail.rc
set from=zhutoyearn@.com smtp=smtp..com
set smtp-auth-user=zhutoyearn smtp-auth-password=xxxxxxxx smtp-auth=login
[root@web1 ~]# echo "zxg"|mail -s "title" zhutoyearn@.com

        查看邮箱是否收到邮件

       3.开始写脚本

[root@web1 scripts]# cat test16.sh
#!/bin/bash
Freemem=`free -m|awk 'NR==3 {print $NF}'`            #<---获取系统当前的内存值,赋给变量Freemem
CHARS="current memory is $freemem."                #<---定义字符串CHARS变量,作为输出及供邮件正文使用
if [ $Freemem -lt ]                       #<---判断如果小于3850,则执行命令
then
echo $CHARS|tee /tmp/messages.txt        #<---屏幕输出提示,并写入文件
mail -s "`date +%F-%T` $CHARS" zhutoyearn@.com </tmp/messages.txt #发送邮件
fi [root@web1 scripts]# chmod +x test16.sh
[root@web1 scripts]# ./test16.sh
current memory is 3840.
[root@web1 scripts]# cat /tmp/messages.txt
current memory is 3840.

      4.加入crond定时任务

no crontab for root
[root@web1 scripts]# vim /etc/crontab SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root # For details see man crontabs # Example of job definition:
# .---------------- minute ( - )
# | .------------- hour ( - )
# | | .---------- day of month ( - )
# | | | .------- month ( - ) OR jan,feb,mar,apr ...
# | | | | .---- day of week ( - ) (Sunday= or ) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
#monitor sys mem at by zxg
*/3 * * * * root /scripts/test16.sh &>/dev/null
~
~
[root@web1 scripts]# crontab /etc/crontab      #保存使其生效
[root@web1 scripts]# crontab -l
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root # For details see man crontabs # Example of job definition:
# .---------------- minute ( - )
# | .------------- hour ( - )
# | | .---------- day of month ( - )
# | | | .------- month ( - ) OR jan,feb,mar,apr ...
# | | | | .---- day of week ( - ) (Sunday= or ) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
#monitor sys mem at by zxg
*/ * * * * root /scripts/test16.sh &>/dev/null
[root@web1 scripts]#

      进入邮箱验证一下,没有问题

  2、深入if语句(多分支)

    1)分别使用read读入及脚本传参的方式比较两个整数的大小

      方法1 read读入单分支

[root@web1 scripts]# cat test17.sh
#!/bin/bash
read -p "pls input two num:" a b       #<---读入两个输入,分别复制给变量a和b
if [ $a -lt $b ];then             #<---如果$a小于$b,则执行命令
echo "yes,$a less than $b"      #<---打印输出,提醒用户
exit 0                 #<---判断完毕,成功执行,以0值退出脚本,此处如果不退出,则会继续执行下面的if语句,而这时不必要的
fi
if [ $a -eq $b ];then             #<---如果$a等于$b,则执行命令,同理,成功后以0退出脚本
echo "yes,$a equal $b"        
exit
fi
if [ $a -gt $b ];then             #<--如果$a大于$b,则执行命令,同理,成功后以0值退出脚本
echo "yes,$a greater than $b"
exit
fi
[root@web1 scripts]# chmod +x test17.sh
[root@web1 scripts]# ./test17.sh
pls input two num:
yes, less than
[root@web1 scripts]# ./test17.sh
pls input two num:
yes, greater than
[root@web1 scripts]# ./test17.sh
pls input two num:
yes, equal
[root@web1 scripts]#

      方法2,上面方法语法比较乱要写很多if,可以用多分枝语句如下:

[root@web1 scripts]# cat test18.sh
#!/bin/bash
read -p "pls input two num:" a b
if [ $a -lt $b ];then
echo "yes,$a less than $b"
elif [ $a -eq $b ];then
echo "yes,$a equal $b"
else [ $a -gt $b ]
echo "yes,$a greater than $b"
fi
[root@web1 scripts]# chmod +x test18.sh
[root@web1 scripts]# ./test18.sh
pls input two num:
yes, equal 2
[root@web1 scripts]# ./test18.sh
pls input two num:
yes, less than
[root@web1 scripts]# ./test18.sh
pls input two num:
yes, greater than

      方法3 用脚本传参的方式比较整数大小(单分支)

[root@web1 scripts]# cat test19.sh
#!/bin/bash
a=$
b=$
if [ $a -lt $b ];then
echo "yes,$a less than $b"
exit
fi
if [ $a -eq $b ];then
echo "yes,$a equal $b"
exit
fi
if [ $a -gt $b ];then
echo "yes,$a greater than $b"
exit
fi [root@web1 scripts]# chmod +x test19.sh
[root@web1 scripts]# ./test19.sh
yes, less than
[root@web1 scripts]# ./test19.sh
yes, less than
[root@web1 scripts]# ./test19.sh
yes, equal
[root@web1 scripts]# ./test19.sh
yes, greater than
[root@web1 scripts]#

       方法4 用脚本传参的方式比较整数大小(多分支)

[root@web1 scripts]# cat test20.sh
#!/bin/bash
a=$
b=$
if [ $a -lt $b ];then
echo "yes,$a less than $b"
elif [ $a -eq $b ];then
echo "yes,$a equal $b"
else [ $a -gt $b ]
echo "yes,$a greater than $b"
fi
[root@web1 scripts]# ./test20.sh
yes, equal
[root@web1 scripts]# ./test20.sh
yes, greater than
[root@web1 scripts]# ./test20.sh
yes, less than
[root@web1 scripts]#

转载请注明出处:https://www.cnblogs.com/zhangxingeng/p/11158122.html

 

最新文章

  1. SQL SERVER出现大量一致性错误的解决方法
  2. Visual Studio(VS2012) Project&amp;(Solution) 虚拟文件夹 &amp; 物理文件夹
  3. 解决Oracle 11g ORA-01017错误代码
  4. ORA-12569: TNS: 包校验和失败解决方法一例
  5. 在Asp.net MVC使用jqGrid--代码少点再少点
  6. block的语法
  7. 关于android存储
  8. 通达OA 同步中控考勤机 增强版
  9. 设计模式之Composite(组合)模式
  10. PHP开发APP接口---返回数据的封装类
  11. 有一种acm题目叫做,奇葩!
  12. 从键盘或文件中获取标准输入:read命令
  13. 关于STM32在程序中间修改PWM值的总结(原创)
  14. XML编程
  15. [国嵌攻略][158][SPI裸机驱动设计]
  16. 超链接标签绑定JS事件&amp;&amp;不加&quot;javascript:;&quot;导致的杯具
  17. kafka-rest:怎么愉快的build?
  18. JavaScript 集合对象
  19. 开源MSSQL Express Profile 文件
  20. springboot中添加热部署

热门文章

  1. 创建加载bean的实例
  2. MQ消息机制如何确认消费了消息?
  3. 当fixed元素相互嵌套时,父元素会影响子元素的层叠关系,最好不要嵌套使用fixed
  4. Flutter之BLOC
  5. 解决IE浏览器没有网络的情况
  6. 0.9.0.RELEASE版本的spring cloud alibaba sentinel实例
  7. &quot;AttributeError: /lib64/libcrypto.so.1.1: undefined symbol: EVP_CIPHER_CTX_cleanup&quot;
  8. linux记录-安装zabbix监控系统
  9. 一个php创建webservice,并通过c#调用的真实实例(转)
  10. 利用SynchronizationContext.Current在线程间同步上下文(转)