Compare 4 Variables and execute unique code for each result

Asked

Viewed 22 times

-1

Hello, I’m trying to create a comparison where the next action will be different for each variable value someone can help me.

The intention is if the value is true, executes one function if it is false executes another, but need to compare 4 variables in the same block only 1 can be true the others have to return false.

Follow my code:

attributes phones of each server

act1="$vartest1" act2="$vartest2" act3="$vartest3" act4="$vartest4"

if [ "$act1" == "test" ] then echo "Truth" Else echo "False"

Elif [ "$act2" == "false" ] then echo "False" Sleep 1s Else echo "Truth"

Elif [ "$act2" == "false" ] then echo "False" Else echo "Truth"

1 answer

1


You could use the syntax of IF, using the logic of OR, where it will be basically like this

act1="$vartest1" act2="$vartest2" act3="$vartest3" act4="$vartest4"

if [ "$act1" == "teste" ou "$act2" == "true" ] 
  then 
     echo "Verdade" else echo "Falso"
     if["$act2" == "true"]
       sleep 1s else echo "Verdade"

elif [ "$act2" == "false" ] then echo "Falso" else echo "Verdade"

i do not understand shell, but the logic base is the same for all language and system.

  • tested here did not work out in shell we used Elif I wanted to compare the results if 4 different variables only 1 is true it executes the command if not it executes another command.

  • As far as I know it would be by the if syntax same, you could use the case, for example, but I use it in JS do not know if have in Shel

Browser other questions tagged

You are not signed in. Login or sign up in order to post.