-5

Experts,

Need your help to achieve my requirement. I am just starting to learn bash script.

Script:

#!/bin/bash

tick=5
tock=

while :
do
if [ -z "$tick" ]
    then
        echo "All Good"
    else
        echo "call for support"
fi
if [ -z "$tock" ]
    then
        echo "I am good"
    else
        echo "call for support now"
fi
sleep 10
done

Summary: In normal conditions, both the variables, tick, and tock will always be empty. So, when I run this script, it will echo "All Good" and "I am good" every 10 secs. During the issuing state, the variable will have an integer. When it happens, the else condition will get hit, and it will echo "call for support" and "call for support now".

What I need is, when the IF condition fails, the script should ECHO else (call for support & call for support now) from both the IF condition only once and break the loop.

If I give break in the first ELSE, the second IF is not read. Please help me to achieve this.

Thanks in advance.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
U cLoUdz
  • 13
  • 3
  • Did you try to use a AND, like you would do in any programming language? – Renaud Pacalet Jun 10 '22 at 11:07
  • I don't quite understand the logic in your script. You assign values to your variables `tock` and `tick`, but never change these variables afterwards. Therefore, `[ -z "$tick" ]` will always be _true_ (status code 0) and `[ -z "$tock" ]` will always be _false_ (status code 1). – user1934428 Jun 10 '22 at 12:05
  • 1
    Does this answer your question? [Two conditions in a bash if statement](https://stackoverflow.com/questions/11370211/two-conditions-in-a-bash-if-statement) – Paul Hodges Jun 10 '22 at 14:54

1 Answers1

0

There are a lot of ways to address this. read up on bash conditionals.
Basically, you just have to make sure you check both conditions interdependently. You are checking them with entirely separate statements.

You could base the loop itself on a combined condition -

while [[ -z "$tick" && -z "$tock" ]]; do echo all good; sleep $someTime; done
echo call support

or make one condition internal to the other if you want more detailed logging...

while :
do if [ -n "$tick" ]
   then echo "Ticking..."
        if [ -n "$tock" ]
        then echo "BOOM! call for support now"
             break
        fi
   fi
done

if statements in bash are pretty flexible. If you know they are integers you can even evaluate them mathematically.

while : ; do 
  if ((tick && tock)); 
  then echo "call support!"; break; 
  else date; sleep $delay;
  fi
done

Just make sure you check both collectively (with or without log output at various steps, as appropriate to your needs) in a structure that best fits your requirements.

addendum

If you always need to check both, and exit if either have a value, but also always want to report if BOTH have a value, use an OR followed by an AND.

while : ; do 
  if ((tick || tock));                   # is either true?
  then printf "call support! "           # primary message
       echo "tick='$tick' tock='$tock'"; # relevant info
       if ((tick && tock));              # are BOTH true?
       then echo "BOTH triggers - call support NOW!" # extra message
       fi
       break;  # exit the loop either way? then do here BELOW 2nd IF
  else date; sleep $delay;
  fi
done

If you need to know which for extra logic, you can still use OR for higher efficiency before checking more carefully on a hit.

while : ; do 
  if ((tick || tock));                               # is either true?
  then echo "call support!"                          # primary message
       if ((tick)); then echo "tick='$tick'"; fi     # relevant action block
       if ((tock)); then echo "tock='$tock'"; fi     # relevant action block
       if ((tick && tock));                          # are BOTH true?
       then echo "BOTH triggers - call support NOW!" # extra actions
            break;  # ONLY exit the loop if BOTH true? do INSIDE here    
       fi
  else date; sleep $delay;
  fi
done

Using ((tick||tock)) means if $tick is empty it will test $tock, which is 2 checks each loop, which isn't bad. Once either hits, dive deeper to see what actually happened in little steps as needed.

Paul Hodges
  • 13,382
  • 1
  • 17
  • 36
  • In my scenario, both variables will be empty. During issue time, those variables will be filled with an integer value. Either both variable will have an integer or anyone will have an integer hinting at a problem. so, whenever the IF condition fails, it should go to 1st IF else >>> break the first loop >>> then go to the second IF >> else>>break loop.I want to monitor both the IF conditions every 10secs. – U cLoUdz Jun 13 '22 at 07:15
  • So always exit if either have a value, but make sure both check for the other first? Or always/only check 2 if 1? – Paul Hodges Jun 13 '22 at 17:01
  • I always want to monitor both the IF conditions and I cannot do this in the same IF condition AFAIK. Whenever the first IF fails>>hit Else>>break>>check second IF condition>>if hit ELSE>> break loop. – U cLoUdz Jun 14 '22 at 11:17
  • "first IF fails>>hit Else>>break>>check second IF" - this isn't going to work. As soon as the first IF issues a break, you have exited the loop. If you have coe to execute in the second IF, do that before the break. If it only exits the loop if BOTH conditions are true, do it inside the SECOND IF, but NOT elsewhere in the first - just make the second INSIDE the first one. – Paul Hodges Jun 14 '22 at 15:31