加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

shell 流程控制

发布时间:2020-12-15 21:13:52 所属栏目:安全 来源:网络整理
导读:分支语句 if else-if else #! /bin/ basha = 10 b = 20 if [ $a == $b ] then echo " a 等于 b " elif [ $a - gt $b ] then echo " a 大于 b " elif [ $a - lt $b ] then echo " a 小于 b " else echo " 没有符合的条件 " fi case #! /bin/ basha = 4 case

分支语句

  • if else-if else
#! /bin/bash

a=10
b=20
if [ $a == $b ]
then
   echo "a 等于 b"
elif [ $a -gt $b ]
then
   echo "a 大于 b"
elif [ $a -lt $b ]
then
   echo "a 小于 b"
else
   echo "没有符合的条件"
fi
  • case
#! /bin/bash

a=4

case $a in 
    1) echo a is 1
        ;;
    2) echo a is 2
        ;;
    3|4) echo a is 3 or 4
        ;;
    *) echo a is others
        ;;
esac

?

循环语句

  • for 循环

  shell for 循环

  • while 循环
#! /bin/bash

i=1
while [ $i -le 5 ] 
do
    echo $i
    let "i++"
done
  • until 循环
#! /bin/bash

a=0

until [ ! $a -lt 10 ]
do
    echo $a
    a=`expr $a + 1`
done

?

跳出循环

  • break
  • continue

总结

  • case 语句中的 ;; 是不能省略的,不会出现C代码中的,不加break,跳到下一个case中继续执行的写法
  • break、continue 只会用于跳出循环

拓展

  • Conditional expressions are used by the [[ compound command and the test and [ builtin commands.
  • The shell allows arithmetic expressions to be evaluated,as one of the shell expansions or by using the (( compound command,the let builtin,or the -i option to the declare builtin
    $ cat demo.sh 
    #! /bin/bash
    int=1
    while (( $int <= 5))     #for ((;;)) 循环也可以这么写
    do
        echo $int
        let "int++"
    done
    
    [email?protected]:~/demo/bash/test$ ./demo.sh 
    1
    2
    3
    4
    5

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读