Shell script to take in 3 inputs and execute commands using a case statement truth table

Recently I had to execute different commands based on 3 given inputs in a truth table style. Instead of going the nested if statement route that is a little hard to read I implemented a case script that assigns a base 2 value to each ‘truth’ input. This is then used to calculate the truth table value to execute a command against.

so:

All true = 7

True, False, True = 5 etc

Its basically the same as using the numeric values in a chmod statement but instead of evaluating to read, write and execute it evaluates to an input. For example I wanted to check the state of 3 services and perform an action based on that; either stopping or starting certain services.

Below is a test code that can be copied and run against all possible inputs. All you need to do is to input what commands you want to run in the ‘execute functions’ at the top and set x, y and z variables based on your scripted checks. Removing the code lines underneath the ‘remove’ comments and all echo statements will leave a live version of the script.

Its also noted that this code could be simplified by putting a default statement in the case and only leaving in the required values for execution.

#!/bin/sh

executecommand1(){
 # Insert code execute command 1
 echo "Executing command 1"
}

executecommand2(){
 # Insert code to execute command 2
 echo "Executing command 2"
}

#remove in live 
test(){

#insert code to perform required checks and then set to variables as 1 = passed 0 = failed
testx=$1
testy=$2
testz=$3




# check x
if((testx == 1));
then
 x=1
else
 x=0
fi




# check y
if((testy == 1));
then
 y=2
else
 y=0
fi




# check z
if ((testz == 1));
then
 z=4
else
 z=0
fi

# add service available variables together to give truth table result
let truthtableinput=$x+$y+$z

#remove in live
#output the state of input
echo $testx $testy $testz




# handle the inputs as a truth table in a case statement
case $truthtableinput in 
 # (t,t,t)
 7) # do nothing
 echo "doing nothing"
 ;;
 # (t,t,f)
 3) # execute command 1
 
 executecommand1
 ;;
 # (t,f,t)
 5) # execute command 2
 
 executecommand2
 ;;
 # (t,f,f)
 1) # execute command 2
 
 executecommand2
 ;;
 # (f,t,t)
 6) # execute command 1
 
 executecommand1
 ;;
 # (f,t,f)
 2) # do nothing
 echo "doing nothing"
 ;;
 # (f,f,t)
 4) # do nothing
 echo "doing nothing"
 ;;
 # (f,f,f)
 0) # do nothing
 echo "doing nothing"
 ;;
esac

#remove } in live
}

# test all variations / remove in live

test 1 1 1
test 1 1 0
test 1 0 1
test 1 0 0
test 0 1 1
test 0 1 0
test 0 0 1
test 0 0 0
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s