Home > Mobile >  While loop that exit when the condition is met
While loop that exit when the condition is met

Time:01-12

I need to write a while loop in bash script that does exit when the process is ended successfully what I have tried so far is;

VAR=`ps -ef |grep -i tail* |grep -v grep |wc -l`
while true; do
{
if [ $VAR = 0 ]
then
echo "Sending MAils ...."
exit
fi
}
done

CodePudding user response:

use break instead of exit to continue the execution of your script.

Also, there is no need for {.

CodePudding user response:

Your script has numerous errors. Probably try https://shellcheck.net/ before asking for human assistance.

You need to update the value of the variable inside the loop.

You seem to be reinventing pgrep, poorly.

(The regular expression tail* looks for tai, tail, taill, tailll ... What do you actually hope this should do?)

To break out of a loop and continue outside, use break.

The braces around your loop are superfluous. This is shell script, not C or Perl.

You are probably looking for something like

while true; do
   if ! pgrep tail; then
      echo "Sending mails ...."
      break
   fi
done

This avoids the use of a variable entirely; if you do need a variable, don't use upper case for your private variables.

  • Related