Exercise:
3.3 Write a shell Script to determine largest among three integer number.
Script3:
echo “enter three integer number”
read a
read b
read c
if [ $a –ge $b ]
then
if [ $a –ge $c ]
then
echo “$a is largest number”
else
echo “$c is largest number”
fi
elif [ $b –ge $c ]
then
echo “$b is largest number”
else
echo “$c is largest number”
fi
3.4 Write a shell script to determine a given year is leap year or not.
Script4:
echo “enter any year”
read y
if [ $(expr $y % 100) –eq 0 ]
then
if [$(expr $y % 400) –eq 0 ]
then
echo “$y is leap year”
else
echo “$y is leap year”
fi
elif [ $(expr $y % 4 ) –eq 0 ]
then
echo “$y is leap year”
else
1
echo “$y is leap year”
fi
3.5 Write a shell script to print multiplication table of given number using while
statement.
Script5:
echo “enter any num”
read n
i=1;
while [ $i –le $n ]
do
m=$(expr $n \* $i);
echo “$n * $i = $m”
i=$(expr $i + 1);
done
3.6 Write a shell script to compare two string.
Script6:
echo “enter two string”
read a
read b
if [ -z $a ]
then
echo “ First String is empty: Null String”
fi
if [ -z $b ]
then
echo “ First String is empty: Null String”
fi
if [ $a = $b ]
then
echo “Strings are equal: strings Matched”
else
echo “Strings are not equal: Strings not match”
fi
3.7 Write a shell script to read and check the directory exists or not, if not make
directory.
Script7:
echo “enter name of directory”
read dir
if [ -d $dir ]
then
echo “Directory $dir Exits!”
else
mkdir $dir
2
fi
3.8 Write a shell script to read and check the directory exists or not, if not make file.
Script7:
echo “enter name of file”
read filename
if [ -f $filename ]
then
echo “File $filename Exits!”
else
touch $filename
fi
3.9 Write a shell script to implement menu driven program to perform all arithmetic
operation using case statement.
Script9:
echo “enter two integer values”
read a
read b
echo –e “Menu \n 1 for Addition \n 2 for Substraction \n 3 for Multiplication \n 4 for
Division \n 5 for Remainder”
echo “enter choice”
read ch
case $ch in
1) echo “Sum=$(expr $a + $b)”;;
2) echo “Substraction=$(expr $a - $b)”;;
3) echo “Multiplication=$(expr $a \* $b)”;;
4) echo “Division=$(expr $a / $b)”;;
5) echo “Remainder=$(expr $a % $b)”;;
6) echo “invalid Choice:Try Again!”
esac
3.10 Write a shell script to do:
(i). display list of directory contents
(ii). Name of current directory
(iii). Who is logged on
(iv). Long listing of directory contents
according to choice of user.
Script10:
echo –e “Menu \n 1 for listing directory content \n 2 for print name of current
directory \n 3 for Show who is logged on \n 4 Show directory content using long
listing format “
echo “enter your choice “
read ch
case $ch in
1) ls;;
3
2) pwd;;
3) who;;
4) ls –l;;
*) echo “Invalid Choice: Try Again!!”
esac
3.11 Write a shell script to print following pattern.
*
**
***
****
Script11 :
echo “ enter number of rows”
read n
i=1
while [ $i –le $n ]
do
j=1
while [ $j –le $i ]
do
echo –n “*”
j=$(expr $j + 1)
done
echo
i=$(expr $i + 1)
done
3.12 Write a shell script to read the file word by word with serial number.
Script12:
i=1
for line in $( cat file1)
do
echo –e “$i \t ”
echo –e “$line \n”
i=$(expr $i + 1 )
done
3.13 Write a shell script to read the file word by word with serial number.
Script12:
i=1
while read line
do
echo –e “$i \t ”
echo –e “$line \n”
i=$(expr $i + 1 )
done < file
4
3.14 Write a shell script to do
(i). counting number of user logged in
(ii). Printing column list of your current directory
(iii). Running your job after logging out.
Script12:
echo “ Number of users logged in”
i=0
who > file # result of who command is appended to file
while read line
do
i=$(expr $i + 1)
done < file
echo “ $i”
i=1
for line in $( cat file)
do
echo –e “$i \t ”
echo –e “$line \n”
i=$(expr $i + 1 )
done
nohup sort file
3.5 running job after logout
nohup
used to continue runing job / task after running out and appending output to
[Link] output file by default.
Usage: nohup [command_name]
3.6 Important Linux commands
3.6.1 date