Loop until grep does not find the text in a file
Solution 1
From grep
man page:
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.
So if a line is present, the exit status is 0. Since on bash 0 is true (because the standard "successful" exit status of programs is 0) you should actually have something like:
#!/bin/bash
while grep "sunday" file.txt > /dev/null;
do
sleep 1
echo "working..."
done
Why exactly are you piping sleep 1
to echo
? Though it works, it doesn't make much sense. If you wanted them inline you could just write sleep 1; echo "working..."
and if you wanted the echo
to run before the delay, you could have it before the sleep
call like echo "working..."; sleep 1
.
Solution 2
This should do the job:
#!/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
Solution 3
Followings works for me, I dont know why while loop didnt work
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
Related videos on Youtube
Damien-Amen
Updated on September 18, 2022Comments
-
Damien-Amen almost 2 years
I have a file named
file.txt
. The content of the file is as followssunday monday tuesday
I wrote the below script and it loops just fine if the
grep
cannot find the pattern that was mentioneduntil cat file.txt | grep -E "fdgfg" -C 9999; do sleep 1 | echo "working..."; done
But my requirement is that the above script should loop until the text mentioned in the
grep
pattern disappears in thefile.txt
I tried to use the
L
flag with grep. But it didn't work.until cat file.txt | grep -EL "sunday" -C 9999; do sleep 1 | echo "working..."; done
-
Admin over 7 yearsSo essentially , you want to print everything until the line that has "sunday" in it , correct ?
-
Admin over 7 years@Serg: No. I want to execute the
do
part until thegrep
part does not findsunday
in thefile.txt
. Assume that some process modifies thefile.txt
and removedsunday
from the file while theuntil.....
statement is running
-
-
Sergiy Kolodyazhnyy over 7 yearsJust use
grep "sunday" file.txt
,avoid useless use of cat -
Sergiy Kolodyazhnyy over 7 yearsAlso,
grep
can be redirected to/dev/null
or use--quiet
option -
IanC over 7 yearsAnother cat saved from cat abuse! Thanks @Serg. I corrected the code, just a note:
--quiet
option would actually break the code here, because then the exit status would always be 0. Redirecting the output to/dev/null
should do it though! -
IanC over 7 years@Serg I think in that case the loop wouldn't even start, because even when a match is found the exit status will be 0 and then reversed to 1 breaking out of the loop.
-
IanC over 7 years@Serg You're right! It does work with
--quiet
too. I misinterpreted thegrep
manual. It says with--quiet
the output will also be 0 when an error occurs, but the output is still 1 if a match is not found! -
keiki over 5 yearsIf you wanna have the reverse functionality (loping while it's NOT there) just replace while with until keyword.