【问题标题】:bash while loop with mixed condition具有混合条件的 bash while 循环
【发布时间】:2012-01-03 11:21:17
【问题描述】:

我的意思不是多个条件,好吧,它也是混合的:

while [[ read line ] && [ "$GoodURL" == "false" ]]

正确的形式是什么?这是一个在文本文件上逐行运行的while循环, 我想用$GoodURL 类型的布尔值来阻止它,请帮忙。 谢谢。

【问题讨论】:

    标签: bash while-loop conditional-statements


    【解决方案1】:
    while read line && [[ "$GoodURL" == "false" ]]
    do 
        echo $line; 
    done
    

    如果您想从文件/管道中读取数据,请务必使用间接寻址,否则您会得到有趣的结果(因为 while 循环在子 shell 中执行,实际上并未使用与周围 shell 相同的环境)

    while read line && [[ "$GoodURL" == "false" ]]
    do 
        echo $line; 
    done < input.txt
    

    【讨论】:

    • 如果我检查进程是否仍在运行,这将不起作用。例如,while read LINE &amp;&amp; [[ pgrep "gnote" &gt; /dev/null ]] 失败。 pgrep gedit 检查应用程序是否仍在运行。`有没有其他方法可以让它工作
    • 啊。对不起。 while read LINE &amp;&amp; pgrep "gnote" &gt; /dev/null。这也失败了。
    • @KhurshidAlam 对我来说就像一个魅力。当然,pgrep 运行 /after/ read 并退出 gnote 不会中断它。你需要问自己的问题。
    猜你喜欢
    • 2022-11-12
    • 2018-03-20
    • 1970-01-01
    • 2022-11-04
    • 1970-01-01
    • 2017-05-03
    • 1970-01-01
    • 2020-08-19
    • 2018-01-21
    相关资源
    最近更新 更多