Predictive Hacks

Tutorial: Control Flow in Bash Scripting

bash

In this tutorial we will provide basic examples of the conditional statements in bash scripting.

Passing Variables to Scripts via Command Line

In the script, we can define the variables with the dollar sign $. By adding a number, we are referring to the variable that we want to pass. So, the $1 is the first variable, the $2 is the second variable and so on. Let’s see an example. Let’s consider the following myexample.sh script.

echo "Hello, I pass the first variable $1 and the second one $2"

And let’s run it by typing:

bash myexample.sh var1 var2

Here, the var1 is assigned to $1 and the var2 to $2 and the output is:

Hello, I pass the first variable var1 and the second one var2

The if-then-else Statement

Below we provide a list of Bash Conditional Expressions

-a fileTrue if file exists.
-b fileTrue if file exists and is a block special file.
-c fileTrue if file exists and is a character special file.
-d fileTrue if file exists and is a directory.
-e fileTrue if file exists.
-f fileTrue if file exists and is a regular file.
-g fileTrue if file exists and its set-group-id bit is set.
-h fileTrue if file exists and is a symbolic link.
-k fileTrue if file exists and its “sticky” bit is set.
-p fileTrue if file exists and is a named pipe (FIFO).
-r fileTrue if file exists and is readable.
-s fileTrue if file exists and has a size greater than zero.
-t fdTrue if file descriptor fd is open and refers to a terminal.
-u fileTrue if file exists and its set-user-id bit is set.
-w fileTrue if file exists and is writable.
-x fileTrue if file exists and is executable.
-G fileTrue if file exists and is owned by the effective group id.
-L fileTrue if file exists and is a symbolic link.
-N fileTrue if file exists and has been modified since it was last read.
-O fileTrue if file exists and is owned by the effective user id.
-S fileTrue if file exists and is a socket.
file1 -ef file2True if file1 and file2 refer to the same device and inode numbers.
file1 -nt file2True if file1 is newer (according to modification date) than file2, or if file1 exists and file2 does not.
file1 -ot file2True if file1 is older than file2, or if file2 exists and file1 does not.
-o optnameTrue if the shell option optname is enabled. The list of options appears in the description of the -o option to the set builtin (see The Set Builtin).
-v varnameTrue if the shell variable varname is set (has been assigned a value).
-R varnameTrue if the shell variable varname is set and is a name reference.
-z stringTrue if the length of string is zero.
-n stringTrue if the length of string is non-zero.
arg1 OP arg2OP is one of ‘-eq’, ‘-ne’, ‘-lt’, ‘-le’, ‘-gt’, or ‘-ge’. These arithmetic binary operators return true if arg1 is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to arg2, respectively. Arg1 and arg2 may be positive or negative integers. When used with the [[ command, Arg1 and Arg2 are evaluated as arithmetic expressions (see Shell Arithmetic).

Let’s provide an example of a simple “if statement”. The script ifthenelse.sh is the following and it asks the end-user to enter a number between 30-40 and if the guessed number is equal to 37 (-eq) then it returns “You found it!“, else it returns “Wrong answer!“:

echo "Guess my Age!"
echo "#####################"
echo ""
echo "Enter a Number Between 30 and 40: "
read GUESS

if [ $GUESS -eq 37 ]
then
    echo "You found it!"
else
    echo "Wrong answer!"
fi

Let’s run it by guessing the correct age which is 37 and then again by entering 35.

Tutorial: Control Flow in Bash Scripting 1

Note that the else if in Bash scripting is elif as in Python.

The for loop

Let’s say that we want to get the first line of each Bash script in our working directory:

echo "List the first line of all bash scripts within the directory"

SHELLSCRIPTS=`ls *.sh`

for SCRIPT in "$SHELLSCRIPTS"
do
    DISPLAY="`head -n 1 $SCRIPT`"
	echo "File: $SCRIPT - First Lines $DISPLAY"
done
Tutorial: Control Flow in Bash Scripting 2

The Case Statement

Let’s provide an example of the case-statement in Bash:

echo "How shall we proceed"
echo "+++++++++++++++"
echo "Press 1 for the main menu"
echo "Press 2 for help"
echo "Press 3 to exit"
echo ""
echo "Press a number between 1 and 3"

read CHOICE

case $CHOICE in
1)
  echo "You are in the main menu";;
2)
  echo "You asked for help";;
3)
  echo "You exit";;
*)
  echo "Choose a number between 1 and 3";;
esac
Tutorial: Control Flow in Bash Scripting 3

The while loop

Let’s provide an example of a while loop.

echo "This is a while loop example"

COUNT=1

while [ $COUNT -le 10 ]
do
  echo "This is the loop number $COUNT"
  COUNT="`expr $COUNT + 1`"
done
Tutorial: Control Flow in Bash Scripting 4

Execution Operators (&& and ||)

In Bash scripting the AND is && and the OR is ||.The right side of && will only be evaluated if the exit status of the left side is zero (i.e. true). || is the opposite: it will evaluate the right side only if the left side exit status is non-zero (i.e. false). If the test inside evaluates to true, it returns zero; it returns nonzero otherwise. For example:

$ false && echo howdy!

$ true && echo howdy!
howdy!
$ true || echo howdy!

$ false || echo howdy!
howdy!

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Leave a Comment

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

snowflake
Miscellaneous

How to Schedule Tasks in Snowflake

We have started a series of Snowflake tutorials, like How to Get Data from Snowflake using Python, How to Load