在shell中使用UNTIL LOOP读取文件

时间:2017-05-31 09:40:30

标签: bash loops

#!/bin/bash
i=1
cat days.txt | while read days
do
    echo $i $days
    let i++
done

我想将while循环更改为until循环

#!/bin/bash
i=1
until conditions
do
    echo $i $days
    let i++
done

预期结果

  1. 星期一
  2. 星期二
  3. 星期三
  4. Blah Blah Blah

1 个答案:

答案 0 :(得分:0)

while read可以更加笨拙地编写为until ! read

i=1
until ! read -r days; do 
    echo "$i $days"
    i=$(( i + 1 ))
done < file
只要while command成功退出,

command会执行某项操作,而until command会在command退出失败之前执行某些操作。 !用于否定read的退出代码。

相关问题