shell(2) if特殊、 case判断、for循环、while循环、break、continue

            一、 if特殊用法


blob.png

1.if [ -z "$a"]

  #!/bin/bash

  if [ ! -f /tmp/iftest ]

   then

      echo "The derectory is not exist"

      exit

   fi

   n=`wc -l /tmp/iftest`

   if [ -z "$n" ]

   then

   echo error

  else

   echo "yes"

  fi

 blob.png

2. if [ -n "$a" ]

判断值要加双引号且 !-z ==-n




            二、case判断

blob.png


测试脚本:(执行脚本的时候输入数字判断分数是否及格)


 #!/bin/bash

read -p "Please input a number: " n

if [ -z "$n" ]

then

    echo "Please input a number."

    exit 1

fi


n1=`echo $n|sed 's/[0-9]//g'`

if [ -n "$n1" ]

then

 echo "Please input a number."

 exit 1

fi


if [ $n -lt 60 ] && [ $n -ge 0 ]

then

    tag=1

elif [ $n -ge 60 ] && [ $n -lt 80 ]

then

    tag=2

elif [ $n -ge 80 ]  && [ $n -lt 90 ]

then

    tag=3

elif [ $n -ge 90 ] && [ $n -le 100 ]

then

    tag=4

else 

    tag=0

fi

case $tag in

    1)

        echo "分数不合格"

        ;;

    2)

        echo "分数及格"

        ;;

    3)

        echo "分数良好"

        ;;

    4)

        echo "分数优秀"

        ;;

    *)

        echo "输入的数值应该是在 0-100."

        ;; 

esac


blob.png

blob.png



            三、for 循环

范围的符号用 `` 

`seq 范围` 

blob.png


1.测试脚本:(1加到100)内容:

blob.png

结果:

blob.png


2.遍历/etc/下的目录:

内容:

blob.png

结果:

blob.png


for i in `seq 1 3`  == for i  1 2 3

for循环会以空格或回车作为分隔符

例如:/tmp/下有三个文件 1.txt、2.txt和3 4.txt(3 4.txt是一个文件,3和4之间有空格)

当在命令行中执行:for i in `ls /tmp/` do echo $i  ;done 

结果则会出现四个文件这样的显示1.txt、 2.txt、3和4.txt(这是3 4.txt是一个文件却因为空格拆分成两个)




                  四、while循环

blob.png

1.格式:

while 条件 ;do ...;done

如 (1)while :  (死循环)  do ;done

     (2)while true == while 1 ;do...;done

2.案例当系统负载大于10时,发送一封邮件(每分钟发送一次)


#!/bin/bash

while :

do

    load=`w|head -1|awk -F 'load average: ' '{print $2}'|cut -d. -f1`

    if [ $load -gt 10 ]          //负载值比对

    then

        top|mail -s "load is high: $load" [email protected]

        (或者:/usr/lib/zabbix/alertscripts/mail.py [email protected] "load is high: $load") 

    fi

    sleep 30     //添加时间间断,没30秒查一次

done


( load=`w|head -1|awk -F 'load average: ' '{print $2}'|cut -d. -f1`

blob.png

过滤所要所要字符。|sed 's/ //' 把过滤字符前的空格去除

blob.png


3.案例:判断输入的内容是否为数字

blob.png

#!/bin/bash

while :

do

    read -p "Please input a number: " n

    if [ -z "$n" ]

    then

        echo "you need input sth."

        continue

    fi

    n1=`echo $n|sed 's/[0-9]//g'`

    if [ -n "$n1" ]

    then

        echo "you just only input numbers."

        continue

    fi

    break

done

echo $n

首先要判断输入的内容是否为空,如果为空则结束本次循环,继续提醒要输入内容

然后判断输入内容,是否为数字还是英文字符串,直到输入的内容是数字才会结束(跳出)整个流程


            

                break跳出循环

blob.png

 当在脚本中的for或者while的循环中都是可以的。使用break,当条件

满足时就会直接跳出本层的循环(就是跳出使用了break这层的循环)

blob.png

执行结果

blob.png

在使用if作判断时,要在条件范围在[ ]中的左右都要空一格,否则会有

语法错误。


                    continue结束本次循环

!!忽略continue之下的代码,直接进行下一次循环

blob.png

continue 是仅仅的结束满足条件的那一次过程,之后的命令也是会执行的

执行结果:

blob.png

blob.png


                    exit退出整个脚本

当整个流程运行命令,遇到exit时,直接退出脚本。

blob.png



执行结果:

blob.png






猜你喜欢

转载自blog.51cto.com/13589255/2121739
今日推荐