How to write a Boolean variable which succeed and failed inside the if loop in shell script ?

I have if loop with multiple variable value check in if loop. How can i print only if loop satisfied variable and its value in shell script ? I dont want to check each variable in if loop. That makes my script larger.

if [[ "${dm}" -gt 0 || "${cl}" -eq 100 || "${ad}" -eq 0 || "${me}" -gt 8 || "${cp}" -gt 0 ]]
        then
         echo "Only satisfied variable with value"
        else
         echo "Only unsatisfied variable with value"
 fi

sample output:

Varibale cp is 2

The correct syntax would be something something like this, using arithmetic evaluation:

if (( dm > 0 || cl == 100 || ad == 0 || me > 8 || cp > 0 )); then

or if you want to use the test utility, the syntax would be like this:

if [ "${dm}" -gt 0 ] || [ "${cl}" -eq 100 ] || [ "${ad}" -eq 0 ] || [ "${me}" -gt 8 ] || [ "${cp}" -gt 0 ]; then
1 Like

Thanks for your reply, I can correct my syntax. By any chance can you help me on how to print variable and its value which is satisfying the condition in IF loop ?

Thanks,

This will only check if one of the conditions is true, but not which one is true. If you want to find out, which of the expression is true, you will need to check them one by one..

Which means something like this:

if (( dm > 0 || cl == 100 || ad == 0 || me > 8 || cp > 0 )); then
  if (( dm > 0 )); then
    echo "dm > 0 "
  fi
  if (( cl == 100 )); then
    echo "cl == 100 "
  fi
   if (( ad == 0 )); then
    echo "ad == 100"
  fi
  if (( me > 8 )); then
    echo "me > 8 "
  fi
  if (( cp > 0 )); then
    echo "cp > 0 "
  fi
else
  echo "None of the conditions are true"
fi

--
For a shorthand way, you could try something like this:

if (( dm > 0 || cl == 100 || ad == 0 || me > 8 || cp > 0 )); then
  (( dm > 0 ))    && echo "dm > 0 "
  (( cl == 100 )) && echo "cl == 100 "
  (( ad == 0 ))   && echo "ad == 100"
  (( me > 8 ))    && echo "me > 8 "
  (( cp > 0 ))    && echo "cp > 0 "
else
  echo "None of the conditions are true"
fi