shell(1) 结构化命令——流程控制

结构化命令——流程控制,

注意:“命令”,非“关键字”

条件判断— if

1 )简单 if

if command                  command 返回成功——即退出状态为 0 时为真

then

       commands

fi

等价于:

if command; then          #; 只是用于将 then 放到同一行

       commands

fi

2 )完整格式的 if

if command; then

       ...

elif command; then

       ...

else

       ...

fi

条件判断— case

case $variable in

       patten1|patten2)  command1s;;   # 使用 | 表多个可能,使用 )

       patten3) command2s;;                 #commands 最后的语句需使用 ;;

       *) default commands;;                 #*) 表示 default

esac

循环— for

1) 简单 for

for var in list; do   # 此处定义了变量 var ,所以不要使用 $

       ...

done

 

eg:

for test in 12s 2 3 4aa           #test 不使用 $ 12s 等不使用引号!引号用于将引起来的内容作为一个整体

do

   echo $test

done

2) 带有引号时

for test in "12's" 2 3 4aa       #用双引号将单引号括起来,反过来也可以吧( TBD

do

   echo $test

done

3) 带有空格时

for test in "12 s" 2 3 4aa      #同样使用引号

do

   echo $test

done

4) 使用变量列表

list="12 2 3 4aa"          TBD :怎样引用某个列表元素?

for test in $list

do

   echo $test

done  

TBD :列表中有空格怎么表示?没找到办法,改下 IFS 吧。  

列表中增加元素

list=$list" abc"             #使用引号并且要有空格

5) BASH 用作分隔符的字符列表

$IFS internal field seperator ,默认为空格、 tab 、换行,将影响 for 的处理—— TBD 影响 gawk 的是否也是它?

IFS $'/n'      #更改 IFS

 

list="12:2:3:4aa bb"

IFS=$': '        #以 : 及空格作为 IFS

for test in $list

do

   echo $test

done

6) 遍历文件

for file in /home/test/*.log /home/aaa/  #可指定多个文件夹

eg:

for test in /home/xgdxiao/* /home/noth/*

#必须使用通配符,不能简单地写个目录名,可同时指定多个目录

do

   if [ -f $test ]; then          #必须对文件进行测试,否则可能输出“ /home/noth/*

       echo $test

   elif [ -e $test ]; then

       echo -n

    else

       echo $test is nth

   fi

done

7) c 式的 for

for((a = 1; a <10; a++)); do              # 格式比较自由,变量引用无须 $ ,可使用空格,无须转义

    echo $a

done

 

for((a=1, b=2; a<=1 && b> 0; a++,b--)); do   #多个变量

    echo $a $b

done

循环— while

1 )简单形式:

while test command; do         #test command 含义同 if 语句

       ...

done

 

eg:

i=2

while [ $i -gt 0 ]; do

    echo $i

    i=$[$i -1 ]

done

 

2 )多个条件,只有最后一个用于判断:

i=2

while echo $i; [ $i -gt 0 ]; do

    i=$[$i -1 ]

done

循环— until

1 )一般形式

until test commands; do

       ...

done

 

2 )多个条件式,处理方式同 while

i=2

until echo $i; [ $i -gt 0 ]; do # 多条命令的处理与 while 类似

    echo $i;

    i=$[$i -1 ]

done

循环—例子

#!/bin/bash

#IFS.OLD=$IFS

IFS=$'/n'        # 控制下面 for

for sth in `cat /etc/passwd|grep root`; do

    echo $sth

    IFS=:            #控制下面的 for

    for ath in $sth; do

       echo $ath

    done

    IFS=/n           #需要还原 IFS

done

循环— break/continue

break n  #跳出 n 层循环, n 默认为 1

continue n  #要继续的循环级别

 

while [ 1 ] ; do             #TBD: 是否可写成 while 0 ; do

    for((i=0; i<2; ++i)); do

       echo $i

       if [ $i -eq 1 ]; then

           break 2

       fi

    done

done

循环—输出重定向或管道

循环里的输出重定向或管道

for((i=0; i<2; ++i)); do

    echo $i

    if [ $i -eq 1 ]; then   

       break 2    #超出总层次也没关系

    fi

done > a.txt    #直接在循环后加重定向, TBD ,错误输出是否写为 2>a.txt

 

for((i=0; i<200; ++i)); do

    echo $i

done |more             #管道

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值