Linux 当某些脚本在其中执行grep时编辑文件是否有任何效果
我正试图根据文件中不存在的文本做出一些决定,但问题是当我的shell脚本在其中执行grep时,文件将被修改Linux 当某些脚本在其中执行grep时编辑文件是否有任何效果,linux,bash,shell,grep,Linux,Bash,Shell,Grep,我正试图根据文件中不存在的文本做出一些决定,但问题是当我的shell脚本在其中执行grep时,文件将被修改 #!/bin/bash grep -q "decision" /home/tejto/test/testingshell ret_code=$? while [ $ret_code -ne 0 ] do echo $ret_code grep -q "decision" /home/tejto/test/testingshell echo 'Inside While!' s
#!/bin/bash
grep -q "decision" /home/tejto/test/testingshell
ret_code=$?
while [ $ret_code -ne 0 ]
do
echo $ret_code
grep -q "decision" /home/tejto/test/testingshell
echo 'Inside While!'
sleep 5
done
echo 'Gotcha!'
当这个shell脚本启动时,文本“decision”不在文件中,但是当我通过其他bash提示符修改这个文件并将文本“decision”放在其中时,在这种情况下,我的脚本没有接受那个更改,而是在,这是否意味着我的shell脚本会缓存该特定文件?因为您在循环外仅设置了一次
ret\u code
变量,而在下一个grep-q
命令后不会在循环内再次设置它
要修复此问题,您需要:
grep -q "decision" /home/tejto/test/testingshell
ret_code=$?
while [ $ret_code -ne 0 ]
do
echo $ret_code
grep -q "decision" /home/tejto/test/testingshell
ret_code=$?
echo 'Inside While!'
sleep 5
done
echo 'Gotcha!'
或者,您可以按如下方式缩短脚本:
#!/bin/bash
while ! grep -q "decision" /home/tejto/test/testingshell
do
echo $?
echo 'Inside While!'
sleep 5
done
echo 'Gotcha!'
i、 e.无需使用变量,直接在while
条件下使用grep-q
[编辑:Tejendra]
最后一个解决方案不会使用ret_代码,并作为第一个解决方案给出所需的结果。dam..这太明显了..我错过了那部分。我使用ret代码,这样每次文件中不存在“decision”时都会输入while循环。如果你可以这样修改你的脚本,顺便说一句,谢谢你指出错误,不客气。我已编辑以显示这两个版本。@anubhava..我们也可以使用untill而不是while并重用您的第二个答案..我正在使用第三个解决方案编辑您的答案..看一看。顺便说一句,非常感谢您的努力。:)第二个选项
whilegrep-q
应该是while!grep-q
。。。除非只是为了演示如何以这种方式使用命令。。。
#!/bin/bash
until grep -q "decision" /home/tejto/test/testingshell
do
echo $?
echo 'Inside While!'
sleep 5
done
echo 'Gotcha!'