zoukankan      html  css  js  c++  java
  • 【Shell脚本学习18】Shell for循环

    与其他编程语言类似,Shell支持for循环。

    for循环一般格式为:

    for 变量 in 列表
    do
        command1
        command2
        ...
        commandN
    done

    列表是一组值(数字、字符串等)组成的序列,每个值通过空格分隔。每循环一次,就将列表中的下一个值赋给变量。

    in 列表是可选的,如果不用它,for 循环使用命令行的位置参数。

    例如,顺序输出当前列表中的数字:

    1. for loop in 1 2 3 4 5
    2. do
    3. echo "The value is: $loop"
    4. done

    运行结果:

    The value is: 1
    The value is: 2
    The value is: 3
    The value is: 4
    The value is: 5


    顺序输出字符串中的字符:

    1. for str in 'This is a string'
    2. do
    3. echo $str
    4. done

    运行结果:

    This is a string


    显示主目录下以 .bash 开头的文件:

    1. #!/bin/bash
    2. for FILE in $HOME/.bash*
    3. do
    4. echo $FILE
    5. done

    运行结果:

    /root/.bash_history
    /root/.bash_logout
    /root/.bash_profile
    /root/.bashrc
  • 相关阅读:
    absolute之后居中宽度自适应
    定位网页元素(5)
    浮动(4)
    Android的方法和属性(1)
    Activity步骤
    JSP的指令
    边框和边距(3)
    计算机快件键
    字体、文本、背景、列表样式和超链接(2)
    c/s和b/s的区别
  • 原文地址:https://www.cnblogs.com/dongdong230/p/4201518.html
Copyright © 2011-2022 走看看