Unit-4 Shell Programming
Unit-4 Shell Programming
What is a shell?
Technically speaking in UNIX language, it is an interface between the user and operating system
service. A shell provides users with an interface that not only accepts human-readable commands
but also executes them.
An operating system consists of many parts, but the 2 core parts are the kernel and shell, we
know that the kernel is like the atom of the operating system, the sole heart of the operating
system. The kernel works a mediator between the software and hardware.
The shell on the other hand is more like the right hand of the kernel, it takes input from you in
the form of commands, processes it, and then gives an output. The shell is retrieved via the
terminal that runs it. Shell also provides an interface through which a user works on programs
and scripts.
Types of shell
Shell is broadly classified into 2 categories:
The shell can be accessed by using a command-line interface, and we all know what a command
line is. It is nothing but the terminal in Linux – the lean, mean coding machine. The terminal is a
special program to type in the human-readable commands such as “cat”, “ls”, “cd”, “pwd”,
“mkdir”, and a thousand more!
When you execute the commands, the result is then displayed on the terminal to the user. The
terminal is powerful and allows users to store commands in a file and execute them together.
Linux is not the only operating system with a command line, even windows had the command
prompt and so doe mac OS
The command line is a boon for developers and administrators, but it may not be everyone’s cup
of coffee as memorizing so many command sand and an even longer list of options is not an easy
task. That is why there are 2 options for the shell so that users can use what they prefer most.
2. Graphical shell
The graphical shell is nothing but the GUI – Graphical interface design. The graphical shell
provides an interface for manipulating programs based on the graphical user interface. The GUI
allows users to do bath tasks that the terminal can and can not do.
For example, you can do tasks like allowing for operations such as opening, closing, moving,
and resizing windows, as well as switching focus between windows and more which the terminal
can’t. The main advantage of the GUI is that users need not type commands for every small task.
There are various shells that are available for Linux based operating systems, let us look at some
of them:
The bash shell is the most loved and commonly used shell in Linux-based operating systems. It
is a default login shell in Linux systems and in macOS, however, it can also be installed on
Mi9crosoft Windows.
The prompt for the bash shell is a dollar sign ($). The bash shell is a derivative of the Bourne
shell, other derivatives of the Bourne shell include POSIX and Korn.
The Korn shell is also derived from the Bourne shell but is the base for POSIX. It was written,
designed, and developed by David Korn in the early 1980s Some features of KSH are:
3. CSH (C shell)
The C shell was written, designed, and developed at the University of California by Bill Joy.
J8ust like bash has the prompt “$”, chs has the prompt “%”. The C shell was developed to be
very similar to the C programming language, it also includes useful programming features like
in-built support for arithmetic operations
The C shell feature ‘aliases’ and command history which was missing in different types of shells
in Linux like the Bourne shell. The complete pathname for the C shell is /bin/sch.
ZSH (Z shell)
Fish Shell
POSIX
TENEX
To avoid this, the shell can also take in the commands as the input from a file, where we can
compile all of these commands and execute them. These files are called Shell programs or shell
scripts and have the extension of “.sh”.
Shell programs, just like any other programming language in the world, have a specific syntax
and a set of rules. A shell script contains the following elements:
Functions
Easy to use
Portable
Qiuck start
Interactive debugging
Simple up learn
Shell Variables
A shell variable is a character string in a shell that stores some value. It could be an integer,
filename, string, or some shell command itself. Basically, it is a pointer to the actual data stored
in memory. We have a few rules that have to be followed while writing variables in the script
(which will be discussed in the article). Overall knowing the shell variable scripting leads us to
write strong and good shell scripts.
Variable Types
We can discuss three main types of variables:
1) Local Variable:
Variables which are specific to the current instance of shell. They are basically used within the
shell, but not available for the program or other shells that are started from within the current
shell.
For example:
`name=Jayesh`
In this case the local variable is (name) with the value of Jayesh. Local variables is temporary
storage of data within a shell script.
2) Environment Variable:
These variables are commonly used to configure the behavior script and programs that are run by
shell. Environment variables are only created once, after which they can be used by any user.
For example:
3) Shell Variables:
Variables that are set by shell itself and help shell to work with functions correctly. It contains
both, which means it has both, some variables are Environment variable, and some are Local
Variables.
For example:
`$SHELL` = Stores the path to the shell program that is being used.
2_AN
!ABD
$ABC
&QAID
Note: It must be noted that no other special character except underscore can be used in a variable
name because all other special characters have special meanings in Shell Scripting.
Defining Variables
Syntax
Example
num="1"
name="Devil"
These kinds of variables are scalar variables as they could hold one value at a time.
1) Accessing variable
Variable data could be accessed by appending the variable name with ‘$’ as follows:
VAR_1="Devil"
VAR_2="OWL"
echo "$VAR_1$VAR_2"
Output:
DevilOWL
Example of Accessing variable
2) Unsetting Variables
The unset command directs a shell to delete a variable and its stored data from list of variables. It
can be used as follows:
var1="Devil"
var2=23
echo $var1 $var2
unset var1
Output:
DEVIL 23
23
Example of Unsetting Variables
Note: The unset command could not be used to unset read-only variables.
var1="Devil"
var2=23
readonly var1
echo $var1 $var2
var1=23
echo $var1 $var2
Output:
Devil 23
line 8: var1: readonly variable
Devil 23
Keyword Description
fi Ends an if statement
case Used for multi-way branching (like a switch statement in other languages)
continue Skips the current iteration of a loop and moves to the next one
Arithmetic Operators
Relational Operators
Boolean Operators
Bitwise Operators
Increment Operator (++): Unary operator used to increase the value of operand by one.
Decrement Operator (- -): Unary operator used to decrease the value of a operand by
one
2. Relational Operators: Relational operators are those operators which define the relation
between two operands. They give either true or false depending upon the relation. They are of 6
types:
‘==’ Operator: Double equal to operator compares the two operands. Its returns true is
they are equal otherwise returns false.
‘!=’ Operator: Not Equal to operator return true if the two operands are not equal
otherwise it returns false.
‘<‘ Operator: Less than operator returns true if first operand is less than second operand
otherwise returns false.
‘<=’ Operator: Less than or equal to operator returns true if first operand is less than or
equal to second operand otherwise returns false
‘>’ Operator: Greater than operator return true if the first operand is greater than the
second operand otherwise return false.
‘>=’ Operator: Greater than or equal to operator returns true if first operand is greater
than or equal to second operand otherwise returns false
3. Logical Operators : They are also known as boolean operators. These are used to perform
logical operations. They are of 3 types:
Logical AND (&&): This is a binary operator, which returns true if both the operands are
true otherwise returns false.
Logical OR (||): This is a binary operator, which returns true if either of the operands is
true or if both the operands are true. It returns false only if both operands are false.
Not Equal to (!): This is a unary operator which returns true if the operand is false and
returns false if the operand is true.
Bitwise OR (|): Bitwise | operator performs binary OR operation bit by bit on the
operands.
Bitwise XOR (^): Bitwise ^ operator performs binary XOR operation bit by bit on the
operands.
Bitwise complement (~): Bitwise ~ operator performs binary NOT operation bit by bit
on the operand.
Left Shift (<<): This operator shifts the bits of the left operand to left by number of times
specified by right operand.
Right Shift (>>): This operator shifts the bits of the left operand to right by number of
times specified by right operand.
5. File Test Operator: These operators are used to test a particular property of a file.
-b operator: This operator check whether a file is a block special file or not. It returns
true if the file is a block special file otherwise false.
-c operator: This operator checks whether a file is a character special file or not. It
returns true if it is a character special file otherwise false.
-d operator: This operator checks if the given directory exists or not. If it exists then
operators returns true otherwise false.
-e operator: This operator checks whether the given file exists or not. If it exits this
operator returns true otherwise false.
-r operator: This operator checks whether the given file has read access or not. If it has
read access then it returns true otherwise false.
-w operator: This operator check whether the given file has write access or not. If it has
write then it returns true otherwise false.
-x operator: This operator check whether the given file has execute access or not. If it
has execute access then it returns true otherwise false.
-s operator: This operator checks the size of the given file. If the size of given file is
greater than 0 then it returns true otherwise it is false.
Basic Syntax:
variable_name=value
Rules:
Examples:
current_date=`date`
# Alternative method:
current_time=$(date)
In Linux shell scripting, I/O (Input and Output) refers to reading input from the user, files, or
commands and writing output to the terminal, files, or other commands.
-n → Prevents a newline
echo -e "Line1\nLine2"
%s → String
%d → Integer
\n → Newline
Basic Input
read name
Pipes (|) allow passing output from one command as input to another.
Example:
ls -l | grep ".txt"
Here:
ls -l lists files
Conditional Statements: There are total 5 conditional statements which can be used in bash
programming
1. if statement
2. if-else statement
4. if..then..else..if..then..fi..fi..(Nested if)
5. switch statement
if statement
Syntax:
if [ expression ]
then
statement
fi
if-else statement
If specified condition is not true in if part then else part will be execute.
Syntax
if [ expression ]
then
statement1
else
statement2
fi
To use multiple conditions in one if-else block, then elif keyword is used in shell. If expression1
is true then it executes statement 1 and 2, and this process continues. If none of the condition is
true then it processes else part.
Syntax
if [ expression1 ]
then
statement1
statement2
elif [ expression2 ]
then
statement3
statement4
else
statement5
fi
if..then..else..if..then..fi..fi..(Nested if)
Nested if-else block can be used when, one condition is satisfies then it again checks another
condition. In the syntax, if expression1 is false then it processes else part, and again expression2
will be check.
Syntax:
if [ expression1 ]
then
statement1
statement2
else
if [ expression2 ]
then
statement3
fi
fi
switch statement
case statement works as a switch statement if specified value match with the pattern then it will
execute a block of that particular pattern
When a match is found all of the associated statements until the double semicolon (;;) is
executed.
A case will be terminated when the last command is executed.
If there is no match, the exit status of the case is zero.
Syntax:
case in
esac
Example Programs
Example 1:
Implementing if statement
a=10
b=20
if [ $a == $b ]
then
if [ $a != $b ]
then
fi
Output
$bash -f main.sh
a is not equal to b
Shell is an interface of the operating system. It accepts commands from users and interprets them
to the operating system. If you want to run a bunch of commands together, you can do so by
creating a shell script. Shell scripts are very useful if you need to do a task routinely, like taking
a backup. You can list those commands and execute them all with just a single script. Let’s see
how you can create a shell script and run it on Linux.
touch script.sh
Now, this script file is not executable by default, we have to give the executable permission to
this file. Type in
chmod +x script.sh
Now, we will add some commands to this shell script. Open this shell script with any text editor
of your choice (command-line based or GUI based) and add some commands. We will use nano.
Type in
nano script.sh
touch testfile
ls
./script.sh
# This is a comment
echo Testing comments in shell script
User-Defined Variables.
System-defined variables, also called environment variables, are generally Capitalised. You can
view all the current environment variables using the printenv command. User-Defined variables
are set by the user, and they exist only during script execution. You can define a variable by
simply typing its name and assigning a value with = sign and access a variable by adding a $
before the variable name. Variables are demonstrated in the following example script.
echo $USER
variable_name="Geeksforgeeks"
echo $variable_name
Conditional statements
Conditional statements are used to execute a block of code only when certain conditions are met.
Shell scripts support the use of conditional statements. We use comparison operators to check the
conditions. Let’s see a few conditional statements.
If statement
Syntax
if [ condition ]
then
#statements
fi
x=10
y=11
if [ $x -ne $y ]
then
fi
If-else statement
In an if-else statement, you can specify a set of commands to run if the condition is not met.
Syntax
if [ condition ]
then
else
fi
x=10
y=10
if [ $x -ne $y ]
then
else
fi
Loops
Using loops, you can a set of commands over and over again, until a certain condition is met.
Let’s see some of the loops.
While loop
It starts running the specified commands if the condition is true and repeats them until the
condition is false.
Syntax
while [ condition ]
do
#set of statements
done
x=2
while [ $x -lt 6 ]
do
echo $x
x=`expr $x + 1`
done
For loop
In a for loop, the variable iterates over a list of values and ends when there are no more values to
iterate over.
Syntax
do
#statements
done
for var in 2 4 5 8
do
echo $var
done