码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2018-05-29 21:46:14      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:shell

            一、 if特殊用法


技术分享图片

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

 技术分享图片

2. if [ -n "$a" ]

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




            二、case判断

技术分享图片


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


 #!/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


技术分享图片

技术分享图片



            三、for 循环

范围的符号用 `` 

`seq 范围` 

技术分享图片


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

技术分享图片

结果:

技术分享图片


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

内容:

技术分享图片

结果:

技术分享图片


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循环

技术分享图片

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" asldkfls@11.com

        (或者:/usr/lib/zabbix/alertscripts/mail.py 15521787110@163.com "load is high: $load") 

    fi

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

done


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

技术分享图片

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

技术分享图片


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

技术分享图片

#!/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跳出循环

技术分享图片

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

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

技术分享图片

执行结果

技术分享图片

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

语法错误。


                    continue结束本次循环

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

技术分享图片

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

执行结果:

技术分享图片

技术分享图片


                    exit退出整个脚本

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

技术分享图片



执行结果:

技术分享图片






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

标签:shell

原文地址:http://blog.51cto.com/13589255/2121739

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!