Task 2
Task 2
SHELL SCRIPT:
A shell script is a list of commands in a computer program that is run by the UNIX shell which
is a command line interpreter.
It usually has comments that describes the steps
Operations performed by shell scripts are program execution, File manipulation and text
printing
VARIABLES:
The shell enables us to create, assign and delete variables.
Variable names must contain only letters, numbers or the underscore
Defining Variables:
Variable_name = variable_value
Variable can be accessed by using a dollar sign ($) as a prefix for variable_name
After a variable is marked read-only, its value cannot be changed
Unsetting a variable directs the shell to remove the variable from the list of the variables.
Unset command cannot be used on the variables which are marked read-only
READ STATEMENT:
The Read statement is used to get input from user and store the data to variable
Syntax: read variable1, variable2, …., variable
ECHO STATEMENT:
The Echo statement is used to display text or value of variables on the screen.
Syntax: echo [options] [string, variables…]
Output:
2 160120733057
ARITHMETIC OPERTIONS:
Demonstration for basic Arithmetic operations:
Code:
Output:
else
statement2
fi
- If-elif-else-fi
if [ expression ]; then
statement1
elif [ expression ]
then
statement2
else
statement3
fi
- Nested if-else
if [ expression ]
then
statement1
if [ expression ]
then
statement
else
statement
fi
else
statement
fi
Demo Program for If-else Statement:
Output:
case $var in
Pattern 1) Statement 1;;
Pattern n) Statement n;;
esac
Demo for case-esac statement:
Output:
LOOPS:
FOR STATEMENT:
The for loop moves through a specified list of values until the list is exhausted.
Syntax:
for varname in list
do
statement
done
WHILE STATEMENT:
Linux scripting while loop is similar to C language while loop
There is a condition in while and commands are executed till the condition is valid. Once
condition becomes false, loop terminates.
Syntax:
while [condition]
do
statement
done
UNTIL STATEMENT:
5 160120733057
The only difference is that until statement executes its code block while its conditional
expression is false, and while statement executes its code block while its conditional
expression is true
Until loop always executes at least once.
Loop while executes till it returns a zero value and until loop executes till it returns non-zero
value.
Syntax:
until [ condition ]
do
commands
done
Demo program for all the loops:
Output:
PROGRAMS:
Description: In this program, we try to read a number from the user and find factorial for that
number
Program:
#!/bin/sh
echo "Enter a number: "
read n
a=1
product=1
while [ $a -le $n ]
do
product=$(( $product * $a ))
a=$(( $a + 1 ))
done
echo "The factorial of number $n is $product"
Output:
Conclusion:
By executing the above shell script, we have successfully found factorial for the number given by
user.
Program-2: Write the Shell Script to check if the given number is even or odd.
Description: In this program, we try to read a number from the user and check whether the given
number is even or odd.
Program:
#!/bin/sh
echo "Enter a number: "
read n
7 160120733057
rem=$(( $n % 2 ))
if [ $rem -eq 0 ]
then
echo "$n is even"
else
echo "$n is odd"
fi
Output:
Conclusion:
By executing the above shell script, we have successfully checked whether the given number is even
or odd.
Description: In this program, we try to read the base and power of a exponent and find the value of
that exponent.
Program:
#!/bin/sh
echo "Enter a number for base: "
read a
echo "Enter a number for power: "
read b
i=1
res=1
while [ $i -le $b ]
do
res=$(( $res * $a ))
i=$(( $i + 1 ))
done
echo "$a^$b = $res"
Output:
Conclusion:
By executing the above shell script, we have successfully found power of a number.
8 160120733057
Program-4: Write a Shell script to check whether the given number is palindrome or not.
Description: In this program, we try to read the number from the user and check whether the given
number is palindrome or not.
Program:
#!/bin/sh
echo "Enter a number: "
read n
temp=$n
re=0
while [ $n != 0 ]
do
r=$(( $n % 10 ))
re=$(( $re * 10 + $r ))
n=$(( $n / 10 ))
done
if [ $re -eq $temp ]
then
echo "The given number is a Palindrome"
else
echo "The given number is not a Palindrome"
fi
Output:
Conclusion:
By executing the above shell script, we have successfully checked whether the given number is
palindrome or not
Program:
#!/bin/sh
echo "Enter a number: "
read n
a=0
b=1
echo "Fibonacci Series: "
echo $a
echo $b
i=3
while [ $i -le $n ]
9 160120733057
do
c=$(( $a + $b ))
echo $c
a=$b
b=$c
i=$(( $i + 1 ))
done
Output:
Conclusion:
By executing the above shell script, we have successfully printed the Fibonacci Series
Program-6: Write a Shell Script to find whether a given number is prime or not.
Description: In this program, we try to read a number from the user and check whether the given
number is prime or not
Program:
#!/bin/sh
echo "Enter a number: "
read n
flag=0
if [ $n -le 1 ]
then
flag=1
else
i=2
a=$(( $n / 2 ))
while [ $i -le $a ]
do
rem=$(( $n % $i ))
if [ $rem -eq 0 ]
then
flag=1;
break;
fi
i=$(( $i + 1 ))
done
fi
if [ $flag -eq 0 ]
then
echo "$n is a prime number"
10 160120733057
else
echo "$n is not a prime number."
Fi
Output:
Conclusion:
By executing the above shell script, we have successfully found whether the number given by the
user is prime or not
Program-7: Write a shell script to check if the given number is an Armstrong number or not.
Description: In this program, we try to read a number from the user and check whether the number
is an Armstrong number or not.
Program:
#!/bin/sh
echo "Enter a number: "
read n
res=0
num=0
temp=$n
while [ $temp -ne 0 ]
do
num=$(( $num + 1 ))
temp=$(( $temp / 10 ))
done
temp=$n
while [ $temp -ne 0 ]
do
r=$(( $temp % 10 ))
re=1
i=1
while [ $i -le $num ]
do
re=$(( $re * $r ))
i=$(( $i + 1 ))
done
res=$(( $res + $re ))
temp=$(( $temp / 10 ))
done
if [ $res -eq $n ]
then
echo "$n is an Armstrong number"
else
echo "$n is not an Armstrong number"
fi
11 160120733057
Output:
Conclusion:
By executing the above shell script, we have successfully checked whether the given number is
Armstrong number or not.
Description: In this program, we try to perform all basic arithmetic operations by demonstrating a
mini calculator.
Program:
#!/bin/sh
echo "Enter two numbers: "
read a
read b
echo "Operations to be Performed: "
echo "1. Add"
echo "2. Sub"
echo "3. Multiply"
echo "4. Division"
echo "Enter your Choice: "
read c
while [ $c -le 4 ]
do
case "$c" in
"1") sum=$(( $a + $b ))
echo "$a + $b = $sum"
;;
"2") diff=$(( $a - $b ))
echo "$a - $b = $diff"
;;
"3") mul=$(( $a * $b))
echo "$a * $b = $mul"
;;
"4") div=$(( $a / $b ))
echo "$a / $b = $div"
;;
esac
echo "Enter your choice: "
read c
done
12 160120733057
Output:
Conclusion:
By executing the above shell script, we have successfully demonstrated the Mini Calculator