Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 当在shell中找不到字符串时,如何停止?_Bash_Shell - Fatal编程技术网

Bash 当在shell中找不到字符串时,如何停止?

Bash 当在shell中找不到字符串时,如何停止?,bash,shell,Bash,Shell,我有这个剧本: #!/bin/bash while [ true ] do if tail -1 /tmp/test | grep 'line3' then echo found sleep 5 else echo not found fi done 它每5秒查找一次第3行。如果未找到第3行,如何使脚本停止?已解决 #!/bin/bash while [ true ] do if tail -1

我有这个剧本:

#!/bin/bash
while [ true ]
do

    if tail -1 /tmp/test | grep 'line3'
    then
        echo found
        sleep 5
    else
        echo not found
    fi
done

它每5秒查找一次第3行。如果未找到
第3行
,如何使脚本停止?

已解决

    #!/bin/bash
while [ true ]
do

    if tail -1 /tmp/test | grep 'line3'
    then
        echo found
        sleep 5
    else
        echo not found
        break
    fi
done

已解决

    #!/bin/bash
while [ true ]
do

    if tail -1 /tmp/test | grep 'line3'
    then
        echo found
        sleep 5
    else
        echo not found
        break
    fi
done

使用逻辑中断。不需要休息

#!/bin/bash

match=1

while [ ${match} -eq 1 ]
do

    if tail -1 /tmp/test | grep 'line3'
    then
        echo found
        sleep 5
    else
        match=0
        echo not found
    fi
done

使用逻辑中断。不需要休息

#!/bin/bash

match=1

while [ ${match} -eq 1 ]
do

    if tail -1 /tmp/test | grep 'line3'
    then
        echo found
        sleep 5
    else
        match=0
        echo not found
    fi
done

有点不清楚为什么在
while[true]
tail | grep
if,then,else
中包含
while[true]
作为
while循环可以使用您的子句作为测试本身:

#!/bin/bash

while tail -1 /tmp/test | grep 'line3'
do
    echo found
    sleep 5
done

echo "not found"

if,then,else
包装在
而[true]
中并没有什么错,只是不太理想。

有点不清楚为什么要将
while[true]
包装在
尾部| grep
if,then,else
作为
while
循环可以使用您的子句作为测试本身:

#!/bin/bash

while tail -1 /tmp/test | grep 'line3'
do
    echo found
    sleep 5
done

echo "not found"

包装
没有什么错,如果,那么,
中的else
而[true]
,它只是不太理想。

你也可以${match}并在loopyup:)中将match设置为1。这样做只是为了简单,用户可以很容易地理解它。你也可以有${match}并在loopyup:)中将match设置为1。这样做只是为了简单,以便用户能够轻松理解。而[true]
仍然是多余的
[true]
不会做你认为它会做的事--
而true
会更惯用。而
而[true]
仍然是多余的
[true]
不会做你认为它会做的事--
而true
会更惯用。谢谢,这个更简单,我会用它:)谢谢,这个更简单,我会用它:)