You are on page 1of 25

Faraz Masood 12MCA19 linux

Q1- Rameshs basic salary is input via keyboard. His dearness allowance is 40% of basic
salary , hra is 20% ofbasic salary. Write a shell script to calculate his gross salary.
echo "Please give Ramesh's Basic salary"
read sal
da=40
ra=20
gross=`echo "scale=2; $sal + $sal * ($da + $ra) / 100.0 "|bc -l`
echo "Gross Saary is " $gross
read xyz

OUTPUT:

Q2- The distance between two cities(km) ,is input thruogh the keyboard.write a shell script
program to convert and print this distance in meters,feet,inches and centimeters.

echo "PLEASE GIVE THE DISTANCE IN KMS "


read km
m=`echo "$km * 1000"|bc -l`
f=`echo "$m * 3.2808399"|bc -l`
i=`echo "$f * 12"|bc -l`
cm=`echo "$m * 100 "|bc -l`
echo Distance in meters is $m in feet is $f in inches is $i and centimeter is $cm
read xyz

OUTPUT:

Q3 Length and breadth of the rectangle and radius of the circle are input through the
keyboard.Write a shell script to calculate the area & perimeter of rectangle and area &
circumference of the circle.

echo "PLEASE GIVE LENGTH AND BREADTH OF RECTANGLE"


read len
read bre
echo "Please give area of circle"
read rad
recarea=`echo "$len * $bre" |bc -l`

1
Faraz Masood 12MCA19 linux

perimeter=`echo "2 * ($len + $bre)" |bc -l`


areacir=`echo "$rad * 22 / 7 * $rad"|bc -l`
circum=`echo "$areacir / $rad * 2"|bc -l`
echo "RECTANGLE :AREA IS $recarea PERIMETER IS $perimeter"
echo "CIRCLE :AREA IS $areacir CIRCUMFERENCE IS $circum"
read xyz

OUTPUT:

Q4 If the five digit number is input via keyboard. Write a shell script to calculate sum of its
digit.

echo "Please give the five digit number"


read num
temp=$num
sum=0
if test $num -gt 9999 -a $num -lt 1000000
then
while test $num -gt 0
do
x=`expr $num % 10`
sum=`expr $sum + $x`
num=`expr $num / 10`
done
echo Sum of digits of $temp is $sum
fi
read xyz

OUTPUT:

2
Faraz Masood 12MCA19 linux

Q5 Write a shell script which would receive logname during execution, obtain information
about it from /etc/passwd and display information.
echo "Enter Login Name:"
read log
l=`cat /etc/passwd|grep $log|cut -d":" -f1`
echo "Login Names:\n$l"
l=`cat /etc/passwd|grep $log|cut -d":" -f2`
echo "Encrypted Passwords:\n$l"
l=`cat /etc/passwd|grep $log|cut -d":" -f3`
echo "UID: $l"
l=`cat /etc/passwd|grep $log|cut -d":" -f4`
echo "Default Group IDs:\n$l"
l=`cat /etc/passwd|grep $log|cut -d":" -f5`
echo "Other Informations:\n$l"
l=`cat /etc/passwd|grep $log|cut -d":" -f6`
echo "Home Directorys:\n$l"
l=`cat /etc/passwd|grep $log|cut -d":" -f7`
echo "Shells:\n$l"
read xyz

OUTPUT:

3
Faraz Masood 12MCA19 linux

Q6 Write a shell script which will receive filename or the file name with its full path during
execution.The script should obtain information about this file as given by ls -l and display it.
echo "Please give the file name "
read fil
echo -n "PERMISSIONS ARE "
ls -l $fil |cut -d" " -f1
echo -n "CHARACTERS ARE "
ls -l $fil |cut -d" " -f5
echo -n "MODIFIED IN "
ls -l $fil |cut -d" " -f6-8
read xyz

OUTPUT:

Q7 In a company,an employee is paid as under:


If his basic sal is < 1500 then hra=10% of sal & da=90% of basic. If sal >= 1500 then hra=rs
500 &da=98% of basic sal.if the employee salary is input through the key board write a
program to find his gross salary.

echo "please give the basic salary"


read sal
if test $sal -lt 1500
then
net=`echo "scale=2;$sal + (10 + 90) * $sal / 100"|bc -l`
else
net=`echo "scale=2;$sal + (98 * $sal / 100) + 500"|bc -l`
fi
echo "Gross Salary is $net"
read xyz

OUTPUT:

4
Faraz Masood 12MCA19 linux

Q8 The marks obtained by a student in 5 different subjects are input via keyboard. Write a
shell script program to calculate the division obtained by the student.

echo "Enter Marks"


echo "C++:"
read a
echo "ADBMS:"
read b
echo "VB:"
read c
echo "COMPILER"
read d
echo "SHELL:"
read e
p=`echo "( $a + $b + $c + $d + $e ) / 5" | bc`
echo "Percentage is: $p"
if test $p -ge 60
then
echo "Congrats!!! First Division"
elif test $p -ge 50 -a $p -lt 60
then
echo "Work Hard!! Second Division"
elif test $p -ge 40 -a $p -lt 50
then
echo "Sorry!! Third Division"
elif test $p -lt 40
then
echo "Ahh!! Fail"
fi
read xyz

OUTPUT:

5
Faraz Masood 12MCA19 linux

Q9 If cost and selling price of an item is input via the keyboard, write a shell script to
determine the seller has made profit or incurred loss. Also determine loss and profit.
echo "Cost Price?? :"
read a
echo "Selling Price?? :"
read b
g=`echo "$b - $a" | bc`
if test $g -gt 0
then
echo "Profit of Rs. $g"
elif test $g -eq 0
then
echo "No Profit No Loss"
elif test $g -lt 0
then
g=`expr $g \* -1`
echo "loss of Rs.$g"
fi
read xyz

OUTPUT:

Q10 Any integer is input through thevkeyboard. Write a shell script program to determine
whether given int number is odd or even number.
echo "enter a number"
read n
x=$n
if [ `expr $x % 2` -eq 0 ]
then
echo "even number"
else
echo "Odd Number"
fi
read xyz

OUTPUT:

6
Faraz Masood 12MCA19 linux

Q11 Write a shell script which recieves any year from the keyboard, and determine weather
the year is leap or not.if no argument is supplied the current year should be assumed.

if test $# -ne 0
then
y=`expr $1 % 4`
if [ $y -eq 0 ]
then
echo "$1 is a Leap Year"
else
echo "$1 is not a Leap Year"
fi
else
d=`date | cut -d " " -f6`
y=`expr $d % 4`
if test $y -eq 0
then
echo "$d is a Leap Year"
else
echo "$d is not a Leap Year"
fi
fi
read xyz

OUTPUT:

Q12 Write a shell script which checks whether the two files contents are same or not. If
they are same second file should be deleted.
if test $# -ne 2
then
echo "Usage is $0 arg1 arg2"
exit 1
fi
arg1=$1
arg2=$2
if cmp $arg1 $arg2
then
echo "Contents are equal"
echo "Second file is deleted `rm $n2`"
else
echo "Contents are different"
fi
read xyz

7
Faraz Masood 12MCA19 linux

OUTPUT:

Q13 Shell script should display the present working directory and report whether your
friend whose loganame aa10 has currently logged in or not. If logged in send a message to
his terminal suggesting a dinner tonight.

"Present Working directory : $PWD"


name=aa10
who|grep "$name">/dev/null
if test $? -eq 0
then
write $name < invite
else
mail $name < request
fi
read xyz

Q14 Write a shell script to find out at how many terminal supplied logname is logged in.
uid=$1
if test $# -eq 0
then
echo "Enter logname"
read uid
fi
echo "Total users are :`who -u|grep "\"$uid"\"|wc -l`"
read xyz

8
Faraz Masood 12MCA19 linux

OUTPUT:

Q15 Shell script can receive an argument one two & three. If the argument supplied is
one display it in bold,if its two reverse it and three blink on the screen.

if test $# -ne 1
then
echo "Usage $0 arg1"
exit 1
fi
arg1=$1
if test $arg1 = "one"
then
echo "`tput bold`$arg1"
echo "`tput sgr0`"
elif test $arg1 = "two"
then
echo "`tput smso`$arg1"
echo "`tput rmso`"
elif test $arg1 = "three"
then
echo "`tput blink`$arg1"
fi
read xyz

OUTPUT:

9
Faraz Masood 12MCA19 linux

Q16 Write a shell script to determine whether the yr is leap yr or not.


echo "Enter a year:"
read a
y=$a
p=`expr $y % 400`
r=`expr $y % 4`
if [ $p -ne 0 -a $r -eq 0 ]
then
echo "$y is a Leap Year"
else
echo "$y is not a Leap Year"
fi
read xyz

OUTPUT:

Q17 Write a shell script that should display good morning / good afternoon/ good
evening depending upon the time at which the user logs in.

b=`date "+%k"`
if test $b -eq 01 -o $b -lt 12
then
echo "Its $b Oclock Good Morning "
elif test $b -eq 12 -o $b -lt 15
then
echo "Its $b Oclock Good AfterNoon"
elif test $b -eq 13 -o $b -lt 19
then
echo "Its $b Oclock Good Evening"
else
echo "Its $b Oclock Good Night"
fi
read xyz

OUTPUT:

10
Faraz Masood 12MCA19 linux

Q18 Write a menu driven program which has the following options:
Contents of /etc/passwd
List of users who have currently logged in
Present working directory
Exit
clear
while true
do
clear
tput cup 10 20
echo " `tput bold`1. Contents of /etc/passwd"
tput cup 11 20
echo "`tput bold` 2. List of users who have logged in"
tput cup 12 20
echo "`tput bold` 3. Present working directory"
tput cup 13 20
echo "`tput bold` 4. Exit"
tput cup 15 20
echo "`tput bold` Please Enter your choice:"
read ans
clear
case $ans in
1) cat /etc/passwd;;
2) who;;
3) pwd;;
4) exit;;
esac
echo "n\n Press any key to continue...."
read key > /dev/null
done

read xyz

OUTPUT:

11
Faraz Masood 12MCA19 linux

Q19 Write a shell script to calculate overtime pay of 10 employees. Overtime is paid at the
rate of rs 12 per hr for every hr worked above 40hrs.

i=1
echo $i
while test $i -le 10
do
echo "Enter the no of hours worked of employee $i:"
read h
if test $h -gt 40
then
ot=`expr $h - 40`
p=`echo "$ot * 12" | bc`
echo "Overtime Hours = $ot"
echo "Overtime Price = Rs. $p"
else
echo "No overtime"
fi
i=`expr $i + 1`
done
read xyz

OUTPUT:

Q20 Write a shell script to calculate factorial value of any number.


echo "enter the value"
read n
i=1
f=1
while test $i -le $n
do
f=`expr $i \* $f`
i=`expr $i + 1`
done
echo "$n! Is = $f"
read xyz

12
Faraz Masood 12MCA19 linux

OUTPUT:

Q21 Write a shell script to find the values of one number raised to the power of another.
echo "Enter the number"
read a
echo "Enter power"
read p
i=0
res=1
while test $i -ne $p
do
res=`expr $res \* $a`
i=`expr $i + 1`
done
echo "$a to the power $p is $res"
read xyz

OUTPUT:

Q22 Write a shell script which reports names and sizes of all files in pwd having size > 100
byte.
echo size name of file
ls -l | cut -c 28-32,46- > b1 OUTPUT:
tr -s ' ' < b1 > b2
grep "^ [0-9][0-9][1-9]." b2 | sort -rn
read xyz

13
Faraz Masood 12MCA19 linux

Q23 Write a shell script that checks after every 1min whether your friend has logged in or
not. You want to contact him as soon as he logs in.
echo "Enter User number :"
read un
x=`who | grep $un |wc -l`
if [ $x -ne 0 ]
then
echo "User $un logged in "
else
while [ $x -eq 0 ]
do
sleep 60
x=`who|grep $un|wc -l`
if [ $x -ge 1 ]
then
echo "now user logged in "
write $un >hi
exit
fi
echo "still waiting"
done
fi

OUTPUT:

Q24 Write a shell script to count and report number of entries present in each subdirectory
mentioned in the path which is supplied as command line argument
if test $# -eq 0
then
echo "Enter path"
read path
else
path=$1
fi
for dir in `ls -R $path`
do
if test -e $dir
then
count=`expr $count + 1`
echo $dir
fi
done
echo "There is $count file"

14
Faraz Masood 12MCA19 linux

OUTPUT:

Q25 Write a shell script to print all prime numbers between 1 to 500.
a=0
n=2
while test $n -lt 500
do
i=2
while test $i -lt $n
do
q=`expr $n % $i`
if test $q -eq 0
then
a=1
break 1
else
a=0
i=`expr $i + 1`
continue
fi
done
if test $a -eq 0
then
echo "Prime Number:$n"
n=`expr $n + 1`
else
n=`expr $n + 1`
continue
fi
done
read xyz

OUTPUT:

15
Faraz Masood 12MCA19 linux

Q26 Write a shell script to generate all combinations of 1,2,3 .


i=3
while test $i -gt 0
do
j=3
while test $j -gt 0
do
k=3
while test $k -gt 0
do
echo "($i,$j,$k)"
k=`expr $k - 1`
done
j=`expr $j - 1`
done
i=`expr $i - 1`
done

OUTPUT:

Q27 Write a shell script for renaming each file in the directory such that it will have shell
PID as an extension

for name in `ls`


do
if test -f $name
then
echo assa
i=`basename $name`
mv $name ${i}$$
fi
done
read xyz

16
Faraz Masood 12MCA19 linux

Q28 A file called wordlists consists of several words.Write a shell script which will receive a
list of file names

k=1
l=$#
x=$1
echo $l $x
while test $k -lt $l
do
i=1
j=1
cnt=`wc -l $1 | cut -c1`
cnt=`expr $cnt`
echo $cnt
while [ $i -le $cnt ]
do
str=`head -1 $x`
cnt1=`grep -c $str $x`
c=`expr $i + 1`
tail "-$c" $x > $2
j=`expr $j + 1`
i=`expr $i + 1`
done
shift
done
read xyz

Q29 Write a shell script that deletes all lines containing the word unix in the files.

if test $# -eq 0
then
echo "Usage is $0 n1"
else
echo "Enter a word to delete"
read y
for i in $*
do
grep -v "$y" "$i" > emp
if test $? -ne 0
then
echo "Word Not Matched"
else
cp emp $i
rm emp
fi
done
fi
read xyz

17
Faraz Masood 12MCA19 linux

Q30 Write a shell script that searches file containing word unix.
for file in `ls`
do
grep -l 'unix' $file
done
read xyz

Q31 A shell script receives even number of file names


a=`expr $# % 2`
if test $a -ne 0
then
echo "Enter even number of arguments"
else
c=1
while test $c -lt $#
do
cp $1 $2
shift
shift
c=`expr $c + 2`
done
fi
read xyz

Q32 Write a shell script which displays a list of all files in the current directory to which you
have read,write & execute permission.
for file in *
do
if test -r $file -a -w $file -a -x $file
then
echo $file
fi
done
read xyz

OUTPUT:

18
Faraz Masood 12MCA19 linux

Q33 Write a shell script which will receive any number of file & directory as arguments. It
should check that every argument supplied is a file or dir. If it is a dir it should be reported
& if filename the name of files as well as the number of lines in it should be reported.
for i in $*
do
if test -d $i
then
echo "Directory $i"
else
echo "file $i contains `cat $i | wc -l` line"
fi
done
read xyz

OUTPUT:

Q34 The shell script should check whether such files already exist. If do they should be
reported. If not then check if sub-directory called mydir exist in current dir. If exist then
report it with the number of files in it or else create it.

for i in $*
do
if test -f $i
then
echo "File $i is already exist"
elif test -d mydir
then
cnt=0
cd mydir
for j in `ls`
do
if test -f $j
then
cnt=`expr $cnt + 1`
fi
done
echo "Mydir exist and contains $cnt files"
else
mkdir mydir
cd mydir
> $i

19
Faraz Masood 12MCA19 linux

fi
done

read xyz

OUTPUT:

Q35 Suppose a user has renamed some files in current directory. Write a shell script to
search all such files & rename them such that they do not contain shell PID.

a=`echo $$`
echo $a
ls | grep $a > mvl.lst
c=`wc -l mvl.lst | cut -d" " -f1`
while test $c -gt 0
do
old=`sed '1q' mvl.lst`
new=`sed '1q' mvl.lst | cut -d"." -f1`
mv $old $new
sed '/'$old'/d' mvl.lst > mvl.lst
mv mvl.lst mvl.lst
c=`expr $c - 1`
done

read xyz

Q36 Write a shell script to identify all zero byte files in current directory and delete them.
for file in *
do
x=`ls -s $file|cut -c1`
if test $x = "0"
then
echo "Delete $file [y/n] \c"
read ans
while test $ans = ""
do
read ans
done
if test $ans = "y" -o $ans = "Y"
then
rm -f $file
echo "$file Deleted "
fi

20
Faraz Masood 12MCA19 linux

fi
done

read xyz

OUTPUT:

Q37 Write a shell script that would display a message terminal locked on screen & wait for
the user to hit a key. On receiving key it should accept password & then unlock the terminal
if the password matches
echo "Terminal Locked"
echo "Enter the password:"
stty -echo
read password1
stty echo
echo "Confirm password:"
stty -echo
read password2
stty echo
if test $password1 != $password2
then
echo "Incorrect Password:"
echo "Terminal cannot be locked"
exit 1
fi
echo "Terminal Locked"
stty intr ^-
stty quit ^-
stty kill ^-
stty eof ^-
stty stop ^-
stty susp ^-
echo "Enter the password to unlock the terminal"
stty -echo
read password3
if test $password3 != $password1
then
stty echo
echo "incorrect password"
fi
while test $password3 != $password1

21
Faraz Masood 12MCA19 linux

do
echo "Enter the password to unlock the terminal"
stty -echo
read password3
if test $password3 != $password1
then
stty echo
echo "incorrect password"
fi
done
stty echo
stty sane
read xyz

OUTPUT:

Q38 Write a shell script using getopts which would receive one or more options from the
command prompt. Check their validity & display appropriate message depending on the
option selected.
if test $# -eq 1
then
echo "BEFORE CHANGING DIRECTORY IS "
pwd
if test $1 = "-"
then
cd ..
else
cd $1
fi
echo "AFTER CHANGING DIRECTORY IS "
pwd
else
echo "Usage is $0 dir or -"
fi

read xyz

22
Faraz Masood 12MCA19 linux

OUTPUT:

Q39 Write a shell script using getopts which can receive two options a & -p. the option a is
followed by argument CGA or VGA, whereas p option is followed by argument SINGLE or
TWO. Script should report which option have been used by the user as well as their validity
while getopts a:p: choice
do
case $choice in
a) if test $OPTARG = "VGA" -o $OPTARG = "CGA"
then
echo "You entered a with argument $OPTARG"
else
echo "You cant enter with argument $OPTARG"
fi
;;
p)
if test $OPTARG = "SINGLE" -o $OPTARG = "TWO"
then
echo "You entered p with argument $OPTARG"
else
echo "You cant enter with argument $OPTARG"
fi
;;
?) echo "You entered Wrong choice"
esac
done

OUTPUT:

23
Faraz Masood 12MCA19 linux

Q40 write a shell script which works similar to the wc command...


while getopts lwc choice
do
case $choice in
l) wc -l $2
;;
w) wc -w $2;;
c) wc -c $2;;
?) echo "USAGE IS $0 -l or -w or -c ";;
esac
done
read xyz

OUTPUT:

24
Faraz Masood 12MCA19 linux

Q41 Write a shell script which helps to shuffle between directories...


if test $# -eq 1
then
echo "BEFORE CHANGING DIRECTORY IS "
pwd
if test $1 = "-"
then
cd ..
else
cd $1
fi
echo "AFTER CHANGING DIRECTORY IS "
pwd
else
echo "Usage is $0 dir or -"
fi

read xyz

OUTPUT:

THANKYOU

25

You might also like