Basic Shell scripts for Operating system
(1) Write a shell script to generate mark sheet of a
student. Take 3 subjects, calculate and display total marks, percentage and
Class obtained by the student.
clear
echo "Enter the three subject marks for the student"
read m1 m2 m3
sum1=`expr $m1 + $m2 + $m3`
echo "Sum of 3 subjects are: " $sum1
per=`expr $sum1 / 3`
echo " Percentage: " $per
if [ $per -ge 60 ]
then
echo "You get Distinction”
elif [ $per -ge 50 ]
then
echo “You get First class”
elif [ $per -ge 40 ]
then
echo "You get Second class"
else
echo "You get Fail"
fi
OUTPUT
Enter the three subject marks for the student
45
35
45
Sum of 3 subjects are: 125
Percentage : 41.67
You get second class
(2) Write a shell script to find factorial of given number n.
clear
echo "enter a number"
read num
fact=1
while [ $num -ge 1 ]
do
fact=`expr $fact\*
$num`
num=’expr $num – 1’
done
echo "factorial of $n is $fact"
Output:
enter a number
4
Factorial of 4 is 24
(3) Write a shell script which will accept a number b and
display first n prime numbers as output.
echo "Enter a number: "
read num
i=2
while [ $i -lt $num ]
do
if [ `expr $num % $i` -eq 0 ]
then
echo "$num is not a prime number"
echo "Since it is divisible by $i"
exit
fi
i=`expr $i + 1`
done
echo "$num is a prime number "
Output:
Enter a number: 1879
1879 is a prime number
Enter a number: 119
119 is not a prime number
(4) Write a shell script which will generate first n fibonnacci
numbers like: 1, 1, 2, 3, 5, 13,…
clear
echo "How many
number of terms to be generated ?"
read n
x=0
y=1
i=2
echo "Fibonacci
Series up to $n terms :"
echo "$x"
echo "$y"
while [ $i -lt $n ]
do
i=`expr $i + 1 `
z=`expr $x + $y
`
echo
"$z"
x=$y
y=$z
done
output:-
How many number of terms to be generated ?
5
0,1,1,2,3
(5)Write a menu driven
shell script which will print the following menu and execute the given task.
MENU
Display calendar of
current month
Display today’s date
and time
Display usernames
those are currently logged in the system
Display your name at
given x, y position
Display your terminal
number
Exit
clear
while(i==0)
do
{
echo"enter
choice from menu"
echo"1. Display calendar of current month
2.Display today’s date and time
3.Display usernames those are currently
logged in the system
4.
Display your name at given x, y position
5.
Display your terminal number
6. Exit "
read choice
case "$choice"
in
1)cal;;
2)date;;
3)who;;
4)tput cup 4 5
whoami;;
5)tty;;
6)exit;;
output:-
enter choice
from menu
1. Display calendar of current month
2.Display today’s date and time
3.Display usernames those are currently
logged in the system
4.
Display your name at given x, y position
5.
Display your terminal number
6. Exit "
5
/dev/pts/11
This comment has been removed by the author.
ReplyDeleteIt's really useful
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteNintranci_mi Anthony Gleason https://wakelet.com/@contbanwera771
ReplyDeleteizarthodoc