Loop until grep does not find the text in a file

39,811

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
Share:
39,811

Related videos on Youtube

Damien-Amen
Author by

Damien-Amen

Updated on September 18, 2022

Comments

  • Damien-Amen
    Damien-Amen almost 2 years

    I have a file named file.txt. The content of the file is as follows

    sunday
    monday
    tuesday
    

    I wrote the below script and it loops just fine if the grep cannot find the pattern that was mentioned

    until 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 the file.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
      Admin over 7 years
      So essentially , you want to print everything until the line that has "sunday" in it , correct ?
    • Admin
      Admin over 7 years
      @Serg: No. I want to execute the do part until the grep part does not find sunday in the file.txt. Assume that some process modifies the file.txt and removed sunday from the file while the until..... statement is running
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy over 7 years
    Just use grep "sunday" file.txt ,avoid useless use of cat
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy over 7 years
    Also, grep can be redirected to /dev/null or use --quiet option
  • IanC
    IanC over 7 years
    Another 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
    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
    IanC over 7 years
    @Serg You're right! It does work with --quiet too. I misinterpreted the grep 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
    keiki over 5 years
    If you wanna have the reverse functionality (loping while it's NOT there) just replace while with until keyword.