0% found this document useful (0 votes)
30 views

Shell Script

The document provides information on shell scripting in Linux operating systems. It discusses that shell scripts are text files that contain sequences of commands that can be executed repeatedly by calling the script file. It also describes different reasons for writing shell scripts such as automation, system administration tasks like backups, system monitoring, and adding new functionality to shells. The document further explains various shell scripting concepts like variables, user input, conditional statements, loops, and commands like expr and test. It provides examples to demonstrate the usage of these concepts and commands.

Uploaded by

Pranav Paste
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Shell Script

The document provides information on shell scripting in Linux operating systems. It discusses that shell scripts are text files that contain sequences of commands that can be executed repeatedly by calling the script file. It also describes different reasons for writing shell scripts such as automation, system administration tasks like backups, system monitoring, and adding new functionality to shells. The document further explains various shell scripting concepts like variables, user input, conditional statements, loops, and commands like expr and test. It provides examples to demonstrate the usage of these concepts and commands.

Uploaded by

Pranav Paste
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

SHELL SCRIPTING

SHELL SCRIPTING
• A shell script is a text files that contains a sequence of commands for
a Linux operating system.
• A shell script is usually created for command sequences in which a
user has a need to use repeatedly in order to save time.
• Like other programs , the shell script can contain parameters,
comment and subcommands that shell must follow.
• Users initiate the sequence of commands in the shell script by simply
entering the file name on a command line.
SHELL SCRIPTING
• There are many reasons to write shell scripts-
• To avoid repetitive work and automation
• System administrators use shell scripting for routine
backups
• System monitoring
• Adding new functionality to the shell etc.
Shell Programming
a. To get a Linux shell, you need to start a terminal.
b. To see what shell you have , run
echo $SHELL
c. In Linux, the dollar sign ($) stands for a shell variable.
d. The ‘echo’ command just returns whatever you type in.
Syntax
Shell script is just a simple text file with “.sh” extension , having
executable permission.
Open terminal
Create the file with the following command :
vi filename.sh
Execute the file
sh filename.sh or ./filename.sh or bash filename.sh
Syntax
Variable and Input
A variable is a value you get from somewhere that will be dynamic.
To define a variable , use varname=value o get the value of variable.
The advantage of using variables in shell scripts is that you can use
them in different ways:
1. A single point of administration for a certain value
2. A value that a user provides in some way
3. A value that is calculated dynamically
For input , you can use read in the script.
Variable and Input
Example:
echo “Enter your name.”
read name
echo “Your name is $name”
expr
expr is a command line Unix utility which evaluates an expression and
outputs the corresponding value.
expr evaluates integer or string expression , including pattern matching
regular expression .
The operators available for integers: addition, subtraction,
multiplication, division and modulus for strings.
expr Example
To perform addition of two numbers:
$ expr 3 + 5
Output:
8

To perform subtraction of two numbers:


$ expr 5 - 3
Output:
2
expr Example
To perform multiplication of two numbers:
$ expr 5 \* 3
Output:
15

To perform division of two numbers:


$ expr 10 / 2
Output:
5
expr Example
To increment variable:
$ y = 10
$ y = `expr $y + 1`
$ echo $y

Output:
11
test
The test command is used to check file types and compare values. Test
is used in conditional execution.
It is used for:
• File attributes comparisons
• Perform string comparisons
• Basic arithmetic comparisons
test Syntax
test condition
OR
test condition && true-command
OR
test condition || false-command
OR
test condition && true-command || false-command
test
If you need to compare two string using test command execute
following:
test string1 = string2 && echo command
If strings are equal then your echo command execute
test string1 = string2 || echo command
If string1 and string2 are not equal the echo command execute
test Example
To test which of the two files are newer, use ‘-nt’
test file1 –nt file2
Constructs
• You can control the execution of Linux commands in a shell script
with control structures.
• Control structure allow you repeat commands and to select certain
commands over others.
• A control structure consists of two major components ; a test and
commands.
• If the test is successful, then the commands are executed .
1. If statement
Use if to execute commands only if certain conditions are met.
Conditions are specified in square brackets [ ] followed by a space.
If the condition specified is true , then commands followed by then
statement will be executed , if statement always ends with fi .
Syntax:
if [ condition ]
then
command
fi
2. If….then…else statement
To customize the working of if further, you can use else to indicate what
should happen if the condition is not meet. If condition is not true, then
commands followed by else statement will be executed.
Syntax:
if [ condition ]
then
command
else
command
fi
3. If….elif…else statement
To use multiple conditions in if-else block, make use of elif. If the condition specified is not true,
then the condition followed by elif will be checked and commands followed by elif statement will
be executed .
If condition followed by elif is not true then commands followed by else statement will be
executed.
Syntax:
if [ condition ]
then
command
elif [ command ]
then
else
command
fi
Write a shell script to find lowest number of two
number?
Creation: gedit lowest.sh
Execute: bash lowest.sh

echo “ Enter two numbers:”


read n1 n2
if [ $n1 –lt $n2 ]
then
echo “ First number is lowest”
else
echo “ second number is lowest”
fi
Write a shell script to find greatest of three numbers
?
Creation: gedit greatest.sh
Execute: bash greatest.sh

echo “ Enter three numbers:”


read n1 n2 n3
if [ $n1 –gt $n2 ] && [ $n1 –gt $n3 ]
then
echo “ $n1 is greatest number”
elif [ $n2 –gt $n1 ] && [ $n2 –gt $n3 ]
then
echo “ $n2 is greatest number”
else
echo “ $n3 is greatest number”
fi
For loop
• Sometimes it’s necessary to execute a series of commands, either for
a limited number of times or for an unlimited number of times . In
such cases for loops offer an excellent solution.

Syntax:
for((initialization;condition;increment/decrement))
do
commands
done
Write a shell script to find factorial of a number .

fact=1
echo “Enter a number:”
read n
If [ $n –gt 0 ]
then
for (( i=$n; i>=1; i-- ))
do
fact=`expr $fact \* $i`
done
fi
echo “Factorial of $n is $fact”
while
• You can use while to run a command as long as a condition is meet.
Syntax:
while condition
do
commands
done
Write a shell script to enter a number and
display all the number upto that number
echo “Enter num:”
read n
i=1
while [ $i –le $n ]
do
echo “$i”
i=`expr $i + 1`
done

You might also like