Shell Script
Shell Script
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
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
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