【问题标题】:Skipping a line and reading another line in shell script while loop在循环时跳过一行并在 shell 脚本中读取另一行
【发布时间】:2012-04-25 18:18:03
【问题描述】:

我有一个代码,它使用 while 循环逐行读取文件。在while循环内,我有一定的条件。有没有一种方法可以让我跳过当前行并根据条件读取下一行?让我准确地说:

while read Line
do
    //some sample conditions
    a=$Line
    if [ "a" == "b" ]
        //i want to go to the next line from this point. 
done < **inputfile**

任何帮助将不胜感激。

【问题讨论】:

    标签: shell while-loop


    【解决方案1】:

    【讨论】:

    • 谢谢一群 scibuff !这么晚才回复很抱歉 !你的解决方案奏效了!
    【解决方案2】:

    怎么样:

    while read Line
    do
        # some sample conditions
        a=$Line
        if [ "$a" == "$b" ] # I assume this is not "a" == "b"
            # i want to go to the next line from this point. 
            read Line
            a=$Line
    done < **inputfile**
    

    【讨论】:

    • if 条件可以是任何东西。我想要的是,根据条件,文件读取应该从下一行开始,然后立即执行。您提供的脚本将读取下一行,但它只会反映在条件之后的行。我想要的是,根据条件,应该读取下一行并且操作应该从头开始。
    • 如果您的评论正确,scibuff 的答案就是您要找的。您能否花一些时间接受它或评论为什么这不是您正在寻找的答案?
    猜你喜欢
    • 2011-07-29
    • 1970-01-01
    • 2021-06-02
    • 2011-12-21
    • 1970-01-01
    • 2015-05-08
    • 2019-04-25
    • 1970-01-01
    • 2017-05-09
    相关资源
    最近更新 更多