外壳中的UNTIL LOOP,用于读取文件 [英] UNTIL LOOP in shell for read file

查看:42
本文介绍了外壳中的UNTIL LOOP,用于读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我想在循环到循环之前更改

I want to change while loop into until loop

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

预期结果

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

推荐答案

读取时可以更笨拙地写为直到!阅读:

i=1
until ! read -r days; do 
    echo "$i $days"
    i=$(( i + 1 ))
done < file

while命令执行某项操作(只要 command 成功退出),而 until command 执行某项操作,直到 command 退出不成功.用于取消 read 的退出代码.

while command does something as long as command exits successfully, whereas until command does something until command exits unsuccessfully. The ! is used to negate the exit code of read.

这篇关于外壳中的UNTIL LOOP,用于读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆