Home Content Basics Programming with Shell Scripts

Programming with Shell Scripts

0

This article will introduce you to the basics of programming with shell scripts.

The best thing about GNU/Linux is that it’s packed with utilities that improve productivity immensely. One such utility is shell that can help accomplish complex tasks with just a few sequences of commands. Most of the time, users operate shell in an interactive way. However, it becomes really productive when we leverage the scripting capabilities of shell. In this beginner-friendly article we will take a look at shell scripting. We will use bash shell to demonstrate all the examples. Then we will discuss important concepts like variables, conditional expressions, loops, etc, with simple yet meaningful examples.

First, the shell script
One might ask what a shell script is? Well, it is a plain text file that contains a sequence of shell commands. Let us understand this with a simple example. Given below is a simple ‘echo’ command, which prints a message on standard output:

$ echo “Programming with Shell script”
Programming with Shell script

Now let us create a simple shell script that displays the above messages on the standard output. We will perform the following steps in sequence to achieve this:

1. Open your favourite text editor.

2. Create a new file, namely ‘first_script.sh’, and add the following content to it:

echo “Programming with Shell script”

3. Your file content should look like this:

$ cat first_script.sh
echo “Programming with Shell script”

4. Execute shell script as follows:

$ bash first_script.sh

Programming with Shell script

Wonderful! You have written your first shell script.

Comments
Comments are used to improve the readability of the script. Shell uses the pound (#) symbol for comments. A line beginning with the pound (#) symbol is ignored by the shell interpreter during execution. The script given below shows the usage of the comment:

$ bash first_script.sh

Programming with Shell script

This script generates the following output when it is executed:

$ bash first_script.sh
Programming with Shell script

Note that the output doesn’t contain any line beginning with the pound (#) symbol.

Working with variables
We can define variables to store information, which can be accessed within the shell script. There are certain rules we need to follow while defining variables:

  1. The variable name can contain any combination of letters from A-Z or a-z, digits from 0-9 or an underscore (_) character.
  2. The variable name should start with a letter or an underscore character.
    A few examples of valid variables are given below:
NAME
name
_name_
name123

By convention, shell variables are defined in upper case. Given below is a simple script, which shows the usage of a variable within it:

$ cat greet.sh
NAME=”Narendra”
echo “Welcome $NAME”

In the above example, we have defined the ‘NAME’ variable, which holds the value ‘Narendra’. Note that we have to use a dollar ($) symbol with a variable name to access it. The above script generates the following output when it is executed:

$ bash greet.sh
Welcome Narendra

In this output, the ‘echo’ command has substituted the value of the variable ‘NAME’. Please note that variable names are case sensitive. So all of the variables given below are treated as different variables:
1. NAME
2. Name
3. NaMe, and so on…

Conditional expressions
Like other programming languages, shell supports conditional expressions like – if, if-else and case. Let us understand this with simple examples.

a) if expression: In case we want to verify whether a file exists or not, we can use the if expression, as follows:

$ touch file.txt #this creates a file

$ cat file_existance.sh
if [ -e file.txt ]; then
echo “file.txt file exists”
fi

The script given above generates the following output when it is executed:

$ bash file_existance.sh
file.txt file exists

b) if-else expression: Let us modify the above script to generate output when the file does not exist:

$ rm file.txt

$ cat file_existance.sh
if [ -e file.txt ]; then
echo “file.txt file exists”
else
echo “file.txt file does not exists”
fi

$ bash file_existance.sh
file.txt file does not exists

c) case statement: Shell provides a switch statement-like functionality using the case statement. Let us understand this with the example given below:

$ cat case_statement.sh
read -p “Enter the day: “ DAY

case $DAY in
Mon) echo “Today is Monday”
;;

Tue) echo “Today is Tuesday”
;;

Wed) echo “Today is Wednesday”
;;

*) echo “Unknown day”
;;
esac

The script given above generates the following output when it is executed:

$ bash case_statement.sh
Enter the day: Mon
Today is Monday

$ bash case_statement.sh
Enter the day: Tue
Today is Tuesday

$ bash case_statement.sh
Enter the day: Invalid
Unknown day

In case statement, asterisk (*) denotes a default case.

Looping constructs
Like other programming languages, shell supports looping constructs. In this section, we will discuss four loops supported by shell.

a) for loop: This operates on lists of items and repeats a set of commands for every item in a list. For example, the script given below prints characters from A to Z.

$ cat loop.sh
for char in {A..Z}
do
echo -n “$char “
done

The above script generates the following output when it is executed:

$ bash loop.sh

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

b) while loop: This executes a set of commands until the provided condition is true. For example, the script given below prints numbers from 1 to 10.

$ cat loop.sh
let i=1
while [ $i -le 10 ]
do
echo -n “$i “
let ++i # increment value of i by 1
done

The above script generates the following output when it is executed:

$ bash loop.sh
1 2 3 4 5 6 7 8 9 10

c) until loop: This executes a set of commands as long as the conditional expression evaluates to false. For example, the script given below prints numbers from 1 to 10:

$ cat loop.sh
let i=1
until [ $i -gt 10 ]
do
echo -n “$i “
let ++i # increment value of i by 1
done

The script above generates the following output when it is executed:

$ bash loop.sh
1 2 3 4 5 6 7 8 9 10

d) select loop: This provides an easy way to create a number menu for selection. For example, the script below creates a menu of the days in a week.

$ cat loop.sh
PS3=”Select the day of the week: “
select day in Sun Mon Tue Wed Thu Fri Sat
do
echo “$day selected”
done

This script generates the following output when it is executed:

$ bash loop.sh
1) Sun
2) Mon
3) Tue
4) Wed
5) Thu
6) Fri
7) Sat
Select the day of the week: 1 Sun selected
Select the day of the week: 2 Mon selected
Select the day of the week: 3 Tue selected
Select the day of the week: ^C

So, we have seen how we can get started with the basic knowledge of scripting languages. There are many usages and applications of such scripts with shell. I leave it to your creativity to explore and try out scripting in different scenarios.

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exit mobile version