Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Shell Scripting

The document provides an overview of shell scripting in Linux, explaining what a shell is and the two main types: Bourne Shell and C Shell. It details the process of creating shell scripts, including syntax for arithmetic and relational operators, as well as conditional and looping statements. Additionally, it includes examples of shell scripts and exercises for practice.

Uploaded by

aulakhnoor444
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Shell Scripting

The document provides an overview of shell scripting in Linux, explaining what a shell is and the two main types: Bourne Shell and C Shell. It details the process of creating shell scripts, including syntax for arithmetic and relational operators, as well as conditional and looping statements. Additionally, it includes examples of shell scripts and exercises for practice.

Uploaded by

aulakhnoor444
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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.

There are two main shells in Linux:

1. The Bourne Shell: The prompt for this shell is $ and its derivatives are listed below:

 POSIX shell also is known as sh


 Korn Shell also knew as sh
 Bourne Again Shell also knew as bash (most popular)

2. The C shell: The prompt for this shell is %, and its subcategories are:

 C shell also is known as csh


 Tops C shell also is known as tcsh

What Is Shell Scripting?

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.

Let us understand the steps in creating a Shell Script

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

The following arithmetic operators are supported by Bourne Shell.


Assume variable a holds 10 and variable b holds 20 then −

Operator Description Example


+ (Addition) Adds values on either side of the operator `expr $a + $b` will give 30

- (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

= (Assignment) a = $b would assign value of


Assigns right operand in left operand
b into a

== (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 −

Operator Description Example

-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

Looping Statements | Shell Script


Looping Statements in Shell Scripting: There are total 2 looping statements which can be
used in bash programming
1. while statement
2. for statement

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

Example-1 add two no.

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

1. echo "Enter the first number : "


2. read num1.
3. echo "Enter the second number : "
4. read num2.
5. sum=`expr $num1 + $num2`
6. echo "sum of two value is $sum"

Example-2

#shell script to find the greatest of two numbers

echo "Enter Num1"


read num1
echo "Enter Num2"
read num2

if [ $num1 -gt $num2 ]


then
echo $num1
else
echo $num2
fi

Example-3
#shell script for factorial of a number
#factorial using while loop

echo "Enter a number"


read num

fact=1

while [ $num -gt 1 ]


do
fact=$((fact * num)) #fact = fact * num
num=$((num - 1)) #num = num - 1
done

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.

8. factorial using for loop

You might also like