4.  循环语句:
    Bash Shell中主要提供了三种循环方式:for、while和until。
    for循环声明格式:
    for variable in word_list
    do
        command
    done
    见如下示例脚本:
    /> cat > test7.sh
    for score in math english physics chemist   #for将循环读取in后面的单词列表,类似于Java的for-each。
    do
        echo "score = $score"
    done
    echo "out of for loop"
    CTRL+D
    /> . ./test7.sh
    score = math
    score = english
    score = physics
    score = chemist
    out of for loop

    /> cat > mylist   #构造数据文件
    tom
    patty
    ann
    jake
    CTRL+D
    /> cat > test8.sh
    #!/bin/sh
    for person in $(cat mylist)                 #for将循环读取cat mylist命令的执行结果。
    do
        echo "person = $person"
    done
    echo "out of for loop."
    CTRL+D
    /> . ./test8.sh
    person = tom
    person = patty
    person = ann
    person = jake
    out of for loop.

    /> cat > test9.sh
    for file in test[1-8].sh                        #for将读取test1-test8,后缀为.sh的文件
    do
        if [ -f $file ]                              #判断文件在当前目录是否存在。
        then
            echo "$file exists."
        fi
    done
    CTRL+D
    /> . ./test9.sh
    test2.sh exists.
    test3.sh exists.
    test4.sh exists.
    test5.sh exists.
    test6.sh exists.
    test7.sh exists.
    test8.sh exists.

    /> cat > test10.sh
    for name in $*                                  #读取脚本的命令行参数数组,还可以写成for name的简化形式。
    do
        echo "Hi, $name"
    done
    CTRL+D
    /> . ./test10.sh stephen ann
    Hi, stephen
    Hi, ann

    while循环声明格式:
    while command  #如果command命令的执行结果为0,或条件判断为真时,执行循环体内的命令。
    do
        command
    done
    见如下示例脚本:
    /> cat > test1.sh  
    num=0
    while (( num < 10 ))               #等同于 [ $num -lt 10 ]
    do
        echo -n "$num "
        let num+=1
    done
    echo -e "\nHere's out of loop."
    CTRL+D
    /> . ./test1.sh
    0 1 2 3 4 5 6 7 8 9 
    Here's out of loop.

    /> cat > test2.sh
    go=start
    echo Type q to quit.
    while [[ -n $go ]]                     #等同于[ -n "$go" ],如使用该风格,$go需要被双引号括起。
    do
        echo -n How are you.
        read word
        if [[ $word == [Qq] ]]      #等同于[ "$word" = Q -o "$word" = q ]
        then
            echo Bye.
            go=                        #将go变量的值置空。
        fi
    done
    CTRL+D
    /> . ./test2.sh
    How are you. Hi
    How are you. q
    Bye.

    until循环声明格式:
    until command                         #其判断条件和while正好相反,即command返回非0,或条件为假时执行循环体内的命令。
    do
        command
    done
    见如下示例脚本:
    /> cat > test3.sh
    until who | grep stephen           #循环体内的命令将被执行,直到stephen登录,即grep命令的返回值为0时才退出循环。
    do
        sleep 1
        echo "Stephen still doesn't login."
    done
    CTRL+D

    shift命令声明格式:shift [n]
    shift命令用来把脚本的位置参数列表向左移动指定的位数(n),如果shift没有参数,则将参数列表向左移动一位。一旦移位发生,被移出列表的参数就被永远删除了。通常在while循环中,shift用来读取列表中的参数变量。
    见如下示例脚本:
    /> set stephen ann sheryl mark #设置4个参数变量。
    /> shift                                    #向左移动参数列表一次,将stephen移出参数列表。
    /> echo $*
    ann sheryl mark
    /> shift 2                                 #继续向左移动两位,将sheryl和ann移出参数列表
    /> echo $*
    mark
    /> shift 2                                 #继续向左移动两位,由于参数列表中只有mark了,因此本次移动失败。
    /> echo $*
    mark

    /> cat > test4.sh
    while (( $# > 0 ))                    #等同于 [ $# -gt 0 ]
    do
        echo $*
        shift
    done
    CTRL+D
    /> . ./test4.sh a b c d e
    a b c d e
    b c d e
    c d e
    d e
    e

    break命令声明格式:break [n]
    和C语言不同的是,Shell中break命令携带一个参数,即可以指定退出循环的层数。如果没有指定,其行为和C语言一样,即退出最内层循环。如果指定循环的层数,则退出指定层数的循环体。如果有3层嵌套循环,其中最外层的为1,中间的为2,最里面的是3。
    见如下示例脚本:
    /> cat > test5.sh
    while true
    do
        echo -n "Are you ready to move on?"
        read answer
        if [[ $answer == [Yy] ]]
        then
            break
        else
            echo "Come on."
        fi
    done
    echo "Here we are."
    CTRL+D
    /> . ./test5.sh
    Are you ready to move on? y
    Here we are

    continue命令声明格式:continue [n]
    和C语言不同的是,Shell中continue命令携带一个参数,即可以跳转到指定层级的循环顶部。如果没有指定,其行为和C语言一样,即跳转到最内层循环的顶部。如果指定循环的层数,则跳转到指定层级循环的顶部。如果有3层嵌套循环,其中最外层的为3,中间的为2,最里面的是1。
    /> cat  maillist                       #测试数据文件maillist的内容为以下信息。
    stephen
    ann
    sheryl
    mark

    /> cat > test6.sh
    for name in $(cat maillist)
    do
        if [[ $name == stephen ]]; then
            continue
        else
            echo "Hello, $name."
        fi
    done
    CTRL+D
    /> . ./test6.sh
    Hello, ann.
    Hello, sheryl.
    Hello, mark.

    I/O重新定向和子Shell:
    文件中的输入可以通过管道重新定向给一个循环,输出也可以通过管道重新定向给一个文件。Shell启动一个子Shell来处理I/O重新定向和管道。在循环终止时,循环内部定义的任何变量对于脚本的其他部分来说都是不看见的。
    /> cat > demodata                        #为下面的脚本构造册数数据
    abc
    def
    ghi
    CRTL+D
    /> cat > test7.sh
    if (( $# < 1 ))                                #如果脚本参数的数量小于1,则给出错误提示后退出。
    then
        echo "Usage: $0 filename " >&2
        exit 1
    fi
    count=1
    cat $1 | while read line                   #参数一中的文件被cat命令输出后,通过管道逐行输出给while read line。
    do
        let $((count == 1)) && echo "Processing file $1..." > /dev/tty  #该行的echo将输出到当前终端窗口。
        echo -e "$count\t$line"              #将输出行号和文件中该行的内容,中间用制表符隔开。
        let count+=1
    done > outfile                               #将while循环中所有的输出,除了>/dev/tty之外,其它的全部输出到outfile文件。
    CTRL+D
    /> . ./test7.sh demodata                #只有一行输出,其余的都输出到outfile中了。
    Processing file demodata...
    /> cat outfile
    1       abc
    2       def
    3       ghi

    /> cat > test8.sh
    for i in 9 7 2 3 5 4
    do
        echo $i
    done | sort -n                                #直接将echo的输出通过管道重定向sort命令。
    CTRL+D
    /> . ./test8.sh
    2
    3
    4
    5
    7
    9

转载于:https://blog.51cto.com/andra/815090

linux 命令详解 二十七相关推荐

  1. linux 命令详解 二十四

     11.  数组: Shell中提供了创建一维数组的能力,你可以把一串数字.名字或者文件放在一个变量中.使用declare的-a选项即可创建它们,或者在变量后面增加下标操作符直接创建.和很多其它开发语 ...

  2. linux 命令详解 二十二

    #${variable:=word}的示例,其C语言表示形式为: #    if (NULL == variable) {     #        variable=world;     #     ...

  3. linux中date使用方法,linux命令详解date使用方法(计算母亲节和父亲节日期脚本示例)...

    linux命令详解date使用方法(计算母亲节和父亲节日期脚本示例) 发布于 2016-02-07 15:58:40 | 108 次阅读 | 评论: 0 | 来源: 网友投递 LinuxLinux是一 ...

  4. Linux命令详解:md5sum 命令

    Linux命令详解:md5sum 命令 一.md5 算法介绍 二.md5sum 命令使用说明 三.md5sum 命令帮助 四.md5sum 命令选项.参数 语法 选项 参数 五.md5sum 命令实战 ...

  5. linux命令chgrp,Linux命令详解之–chgrp命令 | Linux大学

    摘要 Linux chgrp命令 可以用来变更文件与目录的所属群组,设置方式采用群组名称或群组识别码皆可. 我们在Linux命令详解之–chown命令 | Linux大学这篇文章中,我们介绍了更改文件 ...

  6. 《Linux命令详解手册》——Linux畅销书作家又一力作

    关注IT,更要关心IT人,让系统管理员以及程序员工作得更加轻松和快乐.鉴于此, 图灵公司引进了国外知名出版社John Wiley and Sons出版的Fedora Linux Toolbox: 10 ...

  7. c linux time微秒_学习linux,看这篇1.5w多字的linux命令详解(6小时讲明白Linux)

    用心分享,共同成长 没有什么比每天进步一点点更重要了 本篇文章主要讲解了一些linux常用命令,主要讲解模式是,命令介绍.命令参数格式.命令参数.命令常用参数示例.由于linux命令较多,我还特意选了 ...

  8. linux下载命令 scp,linux命令详解之scp命令

    作用 scp命令常用于linux之间复制文件和目录. scp是secure copy的缩写, scp是linux系统下基于ssh登陆进行安全的远程文件拷贝命令. 格式 从本地复制到远程 复制文件 sc ...

  9. RAR for Linux 命令详解

    RAR for Linux 命令详解 用法:  rar <命令>-<开关 1> -<开关 N> <压缩文件> <文件...> <@列表 ...

最新文章

  1. EcoTalks预告 | Max Rietkerk:自然斑图与生态系统的恢复力
  2. linux FTP配置详解
  3. Python将py文件生成exe文件
  4. scrum 12.2
  5. ssm整合之六 时间日期装换
  6. MVC基础知识-View
  7. 多次访问redis造成redis连接总是断开的解决方案
  8. 斑马888t打印机墨盒安装_硒鼓?墨盒?究竟哪个才是打印机的“灵魂伴侣”?...
  9. GDAL源码剖析(十二)之GDAL Warp API使用说明
  10. Flutter中Expanded组件不能直接嵌套LitView报错,解决办法
  11. cmd下运行Oracle清屏命令
  12. 元气骑士机器人的成就皮肤_元气骑士:5把特殊“红武”,想要机器人的皮肤,用它就对了!...
  13. resnet101网络结构
  14. 东北大学《材料现代研究方法》复习题及答案
  15. python 008 __ 小斌文档 | 元组
  16. 自媒体:我为什么要写一篇关于睡眠的文章?
  17. 为什么要学习IA/IP/IE?
  18. 什么是MapReduce?MapReduce的运行机制是什么?MapReduce的实现过程
  19. 用友YonSuite与旺店通数据集成对接-技术篇2
  20. js常用实例:qq。。。

热门文章

  1. 出色管理者的时间管理
  2. Cisco HSRP热备份路由器协议配置
  3. finecms设置伪静态后分享到微信不能访问怎么处理
  4. 配置springmvc在其他类中(spring容器外)获取注入bean
  5. html5 的a标签是可以拨电话的,通过其Href属性来实现
  6. SAP S/4HANA现金管理之变
  7. 利用Procdump+Mimikatz获取Windows帐户密码
  8. 简单五子棋问题,java实现
  9. 树莓派实现人脸识别需要做的那些事
  10. Java培训都学什么