Little Girl's Mostly Linux Blog

ConditionalExecution

Conditional execution

This page was last updated on September 28, 2013.

Basic script structure:

Conditional execution is action taken based on a script performing tests to determine whether certain conditions are met. If none of the conditions are met, the script will do nothing or perform a specified action.

This script will perform action one if condition one is met. Otherwise, it will do nothing:

#!/bin/bash

if [ condition one ];
then action one;
fi

This script will perform action one if condition one is met. Otherwise, it will perform action two:

#!/bin/bash

if [ condition one ];
then action one;
else action two;
fi

This script will perform action one if condition one is met, perform action two if condition two is met, or perform action three if neither condition is met:

#!/bin/bash

if [ condition one ];
then action one;
elif [ condition two ];
then action two;
else action three;
fi

Multiple conditions

This script uses double ampersands (which mean “and”) between multiple conditions for each test, so if condition one and condition two are both met, then action one is executed. If condition three and condition four are both met, then action two is executed. Otherwise, action three is executed:

#!/bin/bash

if [ condition one ] && [ condition two ]; then action one;
elif [ condition three ] && [ condition four ]; then action two;
else action three;
fi

This script uses double pipes (which mean “or”) between multiple conditions for each test, so if condition one or condition two is met, then action one is executed. If condition three or condition four is met, then action two is executed. If none of the conditions are met, action three is executed:

#!/bin/bash

if [ condition one ] || [ condition two ]; then action one;
elif [ condition three ] || [ condition four ]; then action two;
else action three;
fi

Layouts

Since some content will be on more than one line, and since each test can contain more than one condition and more than one action, the layout doesn’t always have to be like the examples above. You can choose a layout that fits the way you work, and/or makes it easy for you to examine your scripts to debug them. Below are almost all the possible layouts for a script that has two conditions and one failure, all of which do the same thing:

#!/bin/bash

if [ condition one ];
then action one;
elif [ condition two ];
then action two;
else action three;
fi
#!/bin/bash

if [ condition one ]; then action one;
elif [ condition two ]; then action two;
else action three;
fi
#!/bin/bash

if [ condition one ];
	then action one;
elif [ condition two ];
	then action two;
else action three;
fi
#!/bin/bash

if [ condition one ]; then
	action one;
elif [ condition two ]; then
	action two;
else action three;
fi
#!/bin/bash

if [ condition one ];
	then action one;
elif [ condition two ];
	then action two;
else
	action three;
fi
#!/bin/bash

if [ condition one ];
	then
		action one;
elif [ condition two ];
	then
		action two;
else
	action three;
fi
#!/bin/bash

if
	[ condition one ];
then
	action one;
elif
	[ condition two ];
then
	action two;
else
	action three;
fi

I left out examples using one or more spaces rather than tabs, and I may have missed additional layouts. If you know of any that aren’t shown above, please let me know. ☺

Obligatory Happy Ending

And they all lived happily ever after. The end.

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Comment:

Create a free website or blog at WordPress.com.