我有一个名为file.txt
.文件内容如下
sunday
monday
tuesday
我写了下面的脚本,如果找不到grep
提到的模式,它循环得很好
until cat file.txt | grep -E "fdgfg" -C 9999; do sleep 1 | echo "working..."; done
但我的要求是上面的脚本应该循环,直到模式中提到的文本grep
消失在 file.txt
我尝试L
在 grep 中使用该标志。但这没有用。
until cat file.txt | grep -EL "sunday" -C 9999; do sleep 1 | echo "working..."; done
答案1
从grep
手册页:
EXIT STATUS
Normally the exit status is 0 if a line is selected, 1 if no lines were
selected, and 2 if an error occurred. However, if the -q or --quiet or
--silent is used and a line is selected, the exit status is 0 even if
an error occurred.
因此,如果存在一行,则退出状态为 0。由于在 bash 上 0 为 true(因为程序的标准“成功”退出状态为 0),您实际上应该有类似以下内容:
#!/bin/bash
while grep "sunday" file.txt > /dev/null;
do
sleep 1
echo "working..."
done
你到底为什么要sleep 1
管道echo
?虽然它有效,但没有多大意义。如果您希望它们内联,您可以只编写sleep 1; echo "working..."
,如果您希望echo
在延迟之前运行,您可以在sleep
调用之前将其放在像echo "working..."; sleep 1
.
答案2
这应该可以完成这项工作:
#!/bin/bash
while true ; do
echo "Working..."
result=$(grep -nE 'sunday' file.txt) # -n shows line number
echo "DEBUG: Result found is $result"
if [ -z "$result" ] ; then
echo "COMPLETE!"
break
fi
sleep 1
done
答案3
以下对我有用,我不知道为什么 while 循环不起作用
until echo "$(kubectl -n production get pods -l=app='activemq' -o jsonpath='{.items[*].status.containerStatuses[0].ready}')" | grep -q "true"; do
sleep 10
echo "Waiting for Broker to be ready......................."
done