Operating System
Lab Journal 6
Student Name
__JAVERIA ABBASI___
Enrolment No.
_01-235191-014_
Class and Section
BS(IT)-4B
Title: Linux Shell Programming II
Objectives: To study and learn Linux Shell Programming Commands, Relational & logical
operators, Conditional Statements.
Tools Used: Linux Ubuntu Terminal, geddit editor, VS Code
Task # 1: Write a script to find the smallest number from 3 numbers given by user.
Program:
#/bin/sh
echo "Enter values:"
read a
read b
read c
if [ $a -le $b ]
then
value=$a;
else
value=$b;
fi
if [ $value -le $c ]
then
value=$value;
else
value=$c;
fi
echo "smallest value is:$value";
Output:
Task # 2: Make a simple calculator which has a nice menu e.g press 1 for addition and so on. It
should have following the functions: Addition, subtraction, division, multiplication and modulus.
Program:
#!/bin/sh
echo "Enter values:"
read a
read b
echo "Enter Choice :"
echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
echo "5. Modulas"
read ch
case $ch in
1)r=$(echo "$a + $b" | bc )
;;
2)r=$(echo "$a - $b" | bc )
;;
3)r=$(echo "$a \* $b" | bc )
;;
4)r=$(echo "$a \ $b" | bc )
;;
4)r=$(echo "$a % $b" | bc )
;;
#echo"default value";
esac
echo "Result is = $r"
Output:
Task # 3: Through case write a shell script that can differentiate between vowels and consonants.
Program:
#!/bin/sh
echo "Enter Choice :"
read ch
case $ch in
"a")echo "vowel"
;;
"A")echo "vowel"
;;
"e")echo "vowel"
;;
"E")echo "vowel"
;;
"i")echo "vowel"
;;
"I")echo "vowel"
;;
"o")echo "vowel"
;;
"O")echo "vowel"
;;
"u")echo "vowel"
;;
"U")echo "vowel"
;;
*) echo "not a vowel"
;;
#echo"default"
esac
Output:
Task # 4: Write a script to find whether a particular triangle is equilateral, isosceles or scalene.
Program:
#!/bin/sh
echo "Enter three sides of a triangle:"
read a
read b
read c
if [ $a -eq $b -a $b -eq $c ]
then
echo "it is an equaliteral triangle"
elif [ $a -eq $b -o $a -eq $c -o $b -eq $c ]
then
echo "it is an isosceles triangle"
else
echo " it is an scalene triangle"
fi
Output:
Task # 5: Save the path of any file in a variable and write a script to automatically perform all file
tests on it.
Bonus: Take the filename from user, check if it exists then perform file testing on it, otherwise
echo on screen that file doesn’t exist.
Program:
#!/bin/sh
file="/home/javeriaabbasi/shellscripting/check.txt"
if [ -d $file ]
then
echo "File is a directory"
else
echo "File is not a directory"
fi
if [ -f $file ]
then
echo "File exist but not a directory"
else
echo "File doesnot exist"
fi
if [ -r $file ]
then
echo "user can read the file"
else
echo "user cant read the file"
fi
if [ -e $file ]
then
echo "File exist"
else
echo "File doesnot exist"
fi
if [ -s $file ]
then
echo "File is of non zero size"
else
echo "File is not of zero size"
fi
if [ -w $file ]
then
echo "File is writeable"
else
echo "File is not writeable"
fi
if [ -x $file ]
then
echo "can execute"
else
echo "not executable"
fi
Output:
Submission Date: 12-05-2020 Signature: Ms Umarah Qaseem