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
会更惯用。谢谢,这个更简单,我会用它:)谢谢,这个更简单,我会用它:)