Looping Constructs(from bash manual)

本文详细介绍了Shell脚本中的三种循环结构:until、while和for循环的语法及使用方法,并解释了它们的执行逻辑和返回状态。此外,还特别说明了for循环的一种替代形式及其算术表达式的评估规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

until
The syntax of theuntil command is:
            until test-commands; do consequent-commands; done

Execute consequent-commands as long astest-commands has an exit status which is not zero. The return status is the exit status of the last command executedinconsequent-commands, or zero if none was executed.


while
The syntax of thewhile command is:
            while test-commands; do consequent-commands; done

Execute consequent-commands as long astest-commands has an exit status of zero. The return status is the exit status of the last command executedinconsequent-commands, or zero if none was executed.


for
The syntax of thefor command is:
            for name [ [in [words ...] ] ; ] do commands; done

Expand words, and executecommands once for each memberin the resultant list, with name bound to the current member. If ‘inwords’ is not present, the for commandexecutes thecommands once for each positional parameter that isset, as if ‘in "$@"’ had been specified. The return status is the exit status of the last command that executes. If there are no items in the expansion ofwords, no commands areexecuted, and the return status is zero.

An alternate form of the for command is also supported:

            for (( expr1 ; expr2 ; expr3 )) ; do commands ; done

First, the arithmetic expressionexpr1 is evaluated accordingto the rules described below . The arithmetic expressionexpr2 is then evaluated repeatedlyuntil it evaluates to zero. Each timeexpr2 evaluates to a non-zero value, commands areexecuted and the arithmetic expressionexpr3 is evaluated. If any expression is omitted, it behaves as if it evaluates to 1. The return value is the exit status of the last command inlistthat is executed, or false if any of the expressions is invalid.     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值