Most computer programs, regardless of language, are just a bunch of conditional statements. These conditionals allow the control flow of a program to be altered instead of just running every single line of code all of the time. Outside of computer programming, conditionals are all around us, just a series of decisions being made and their outcomes.
Maybe we really are living inside a simulation. O_O
To run the following shell scripting commands, you will need access to a system with a shell like
zsh available. This is standard issue on Unix-like systems like Linux and macOS and can be accessed on Windows and Chrome OS by way of their respective Linux subsystem offerings.
Even though we’re talking about scripts, the source code we will be writing can be entered directly into your shell prompt and executed. If you happen to goof while typing something in, don’t worry, you can simply hit
CTRL-C to get back to a clean prompt.
Basic conditional structure
Just like all of the conditionals you’ve encountered in other languages, conditionals in shell scripting consist of the same “if”, “else” and “else if” conditions, with slightly different syntax:
GATOR=true SNAKE=false # if if [ $GATOR ]; then echo "We have a gator" fi if [ !$GATOR ]; then echo "We don't have a gator" fi # if / else if [ $GATOR ]; then echo "We have a gator" else echo "We don't have a gator" fi # if / else if / else if [ $SNAKE ]; then echo "We have a snake" elif [ $GATOR ]; then echo "We have a gator" else echo "We don't either" fi
When you’re doing integer conditionals, you use operators like
-ge (equal and greater than or equal to respectively):
GATOR_SCORE = 100; SNAKE_SCORE = 0; if [ $GATOR_SCORE -gt $SNAKE_SCORE ]; then echo "The gator wins" elif [ $GATOR_SCORE -lt $SNAKE_SCORE ]; then echo "The snake wins" else echo "It's a tie" fi
-eqis equal to
-neis not equal to
-gtis greater than
-geis great then or equal to
-ltis less than
-leis less than or equal to
Comparing strings is quite similar to comparing integers, but we have slightly different operators to use:
GATOR="alligator" SNAKE="anaconda" if [ $GATOR = $SNAKE ]; then echo "They are the same"; fi if [ $GATOR != $SNAKE ]; then echo "They are different"; fi; if [ $GATOR = $SNAKE ]; then echo "They are the same"; else echo "They are different"; fi;
You can also check strings for less than or greater than. Instead of being based on numeric value, it’s based on ASCII alphabetical order. When using these operators, you either need to escape them, or use “double brackets”
]] instead of
if [ $GATOR \< $SNAKE ]; then echo "alligator comes before snake"; fi; if [[ $GATOR < $SNAKE ]]; then echo "alligator comes before snake"; fi;
==is equal to
!=is not equal to
<is less than in ASCII alphabetical order
>is greater than in ASCII alphabetical order
While not an exhaustive list of examples, these basic conditionals can still get you pretty far when writing scripts. Combine these conditionals with command-line arguments coming into your script or even user input and you can really take your script to the next level.