Shell Scripting
Shell Scripting
What is a Shell
A shell in a Linux operating system takes input from you in the form of commands, processes
it, and then gives an output. It is the interface through which a user works on the programs,
commands, and scripts. A shell is accessed by a terminal which runs it.
When you run the terminal, the Shell issues a command prompt (usually $), where you can
type your input, which is then executed when you hit the Enter key. The output or the result is
thereafter displayed on the terminal.
1. The Bourne Shell: The prompt for this shell is $ and its derivatives are listed below:
2. The C shell: The prompt for this shell is %, and its subcategories are:
SHELL SCRIPTING is writing a series of commands for the shell to execute. It can combine
lengthy and repetitive sequences of commands into a single and simple script, which can be
stored and executed anytime. This reduces the effort required by the end user.
1. Create a file using a vi editor (or any other editor). Name script file with extension .sh
2. Start the script with #! /bin/sh
3. Write some code.
4. Save the script file as filename.sh
5. For executing the script type bash filename.sh
Arithmetic Operators
- (Subtraction) Subtracts right hand operand from left hand operand `expr $a - $b` will give -10
* (Multiplication) Multiplies values on either side of the operator `expr $a \* $b` will give 200
/ (Division) Divides left hand operand by right hand operand `expr $b / $a` will give 2
% (Modulus) Divides left hand operand by right hand operand and `expr $b % $a` will give 0
returns remainder
== (Equality) Compares two numbers, if both are same then returns [ $a == $b ] would return
true. false.
!= (Not Equality) Compares two numbers, if both are different then returns [ $a != $b ] would return true.
true.
It is very important to understand that all the conditional expressions should be inside square
braces with spaces around them, for example [ $a == $b ] is correct whereas, [$a==$b] is
incorrect.
All the arithmetical calculations are done using long integers.
Relational Operators
Bourne Shell supports the following relational operators that are specific to numeric values.
These operators do not work for string values unless their value is numeric.
For example, following operators will work to check a relation between 10 and 20 as well as
in between "10" and "20" but not in between "ten" and "twenty".
Assume variable a holds 10 and variable b holds 20 then −
-eq Checks if the value of two operands are equal or not; if yes, then the [ $a -eq $b ] is not true.
condition becomes true.
-ne Checks if the value of two operands are equal or not; if values are not
[ $a -ne $b ] is true.
equal, then the condition becomes true.
-gt Checks if the value of left operand is greater than the value of right
[ $a -gt $b ] is not true.
operand; if yes, then the condition becomes true.
-lt Checks if the value of left operand is less than the value of right operand;
[ $a -lt $b ] is true.
if yes, then the condition becomes true.
-ge Checks if the value of left operand is greater than or equal to the value of
[ $a -ge $b ] is not true.
right operand; if yes, then the condition becomes true.
-le Checks if the value of left operand is less than or equal to the value of
[ $a -le $b ] is true.
right operand; if yes, then the condition becomes true.
It is very important to understand that all the conditional expressions should be placed inside
square braces with spaces around them. For example, [ $a <= $b ] is correct whereas, [$a <=
$b] is incorrect.
Conditional Statements | Shell Script
Conditional Statements:
1. if statement
2. if-else statement
3. if..then..else..if..then..fi..fi..(Nested if)
Their description with syntax is as follows:
if statement
This block will process if specified condition is true.
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
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
while statement:- Here command is evaluated and based on the result loop will executed, if
command raise to false then loop will be terminated
Syntax
while command
do
Statement to be executed
done
for statement:- The for loop operate on lists of items. It repeats a set of commands for every
item in a list.
Here var is the name of a variable and word1 to wordN are sequences of characters separated
by spaces (words). Each time the for loop executes, the value of the variable var is set to the
next word in the list of words, word1 to wordN.
Syntax
for var in word1 word2 ...wordn
do
Statement to be executed
done
1. Open file in vi editor with command vi add.sh and write bash script.
2. Then num1 and num2 holds the values of the first and second arguments
respectively.
3. sum holds the addition of and b.
4. Save the file with command :wq!
5. Run the shell script with command bash add.sh
Example-2
Example-3
#shell script for factorial of a number
#factorial using while loop
fact=1
echo $fact
Exercise
1. Write a Shell program to check the given number is even or odd.
2. Write a Shell program to check the given number is palindrome or not.
3. Write a Shell program to check the given number is prime or not.
4. Write a Shell program to find the largest number among three numbers.
5. Write a Shell program to find the sum of digits of a number.
6. Write a Shell program to print the reverse of a number.
7. Write a Shell program to generate Fibonacci series.