Operating System – Unix Shell Scripts Examples
Here in this section of Operating System Long Questions and Answers,We have listed out some of the important Long Questions with Answers on Unix Shell Scripts Examples which will help students to answer it correctly in their University Written Exam.Lists of Unix Shell Scripts Examples that may be asked in Written Exams.
- 1) Write a shell script to scans the name of the command and executes it.
- 2) Write a shell script Which works like calculator and performs below operations Addition , Subtract ,Division ,Multiplication
- 3) Write a shell script to print the pyramid structure for the given number.
- 4) Write a shell script to find the largest among the 3 given numbers.
- 5) Write a shell script to find factorial of given number n.
- 6) Write a shell script to print all prime numbers from 1 to n.
- 7) Write a shell script to reverse a number supplied by a user.
- 8) Write a shell script to find first n Fibonacci numbers like: 0 1, 1, 2, 3, 5, 13,…
9) Write a shell script to check whether the given number is Perfect or not.
- 10) Write a shell script which displays a list of all files in the current directory to which you have read, write and execute permissions
- 11) Write a shell script that deletes all the files in the current directory which are 0 bytes in length.
- 12) Write a shell script to check whether the given string is Palindrome or not.
- 13) Write a shell script to display the digits which are in odd position in a given 5 digit number
- 14) Write a shell script to check given year is leap year or not.
- 15) Write a shell script to find the value of one number raised to the power of another.
Example -1 Write a shell script to scans the name of the command and executes it.
Program:-
read cmd
$cmd
Output:-
cal
February 2019
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29
Example -2 Write a shell script Which works like calculator and performs below operations Addition , Subtract ,Division ,Multiplication
Program:-
echo ” Enter one no.”
read n1
echo “Enter second no.”
read n2
echo “1.Addition”
echo “2.Subtraction”
echo “3.Multiplication”
echo “4.Division”
echo “Enter your choice”
read ch
if [ $ch = “1” ]
then
sum=`expr $n1 + $n2`
echo “Sum =”$sum
elif [ $ch = “2” ]
then
sum=`expr $n1 – $n2`
echo “Sub = “$sum
elif [ $ch = “3” ]
then
echo “Mul = “$sum
elif [ $ch = “4” ]
then
sum=`expr $n1 / $n2`
echo “Div = “$sum
fi
Output:-
32
Enter second no.
12
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter your choice
2
Sub = 20
ii) using while loop and switch statement
i=”y”
while [ $i = “y” ]
do
echo ” Enter one no.”
read n1
echo “Enter second no.”
read n2
echo “1.Addition”
echo “2.Subtraction”
echo “3.Multiplication”
echo “4.Division”
echo “Enter your choice”
read ch
case $ch in
1)sum=`expr $n1 + $n2`
echo “Sum =”$sum;;
2)sum=`expr $n1 – $n2`
echo “Sub = “$sum;;
3)sum=`expr $n1 \* $n2
echo “Mul = “$sum;;
4)sum=`expr $n1 / $n2`
echo “Div = “$sum;;
*)echo “Invalid choice”;;
esac
echo “Do u want to continue ? y/n”
read i
if [ $i != “y” ]
then
exit
fi
done
Output :-
Enter one no.
32
Enter second no.
22
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter your choice
2
Sub = 10
Do u want to continue ? y/n
N
Example -3 Write a shell script to print the pyramid structure for the given number.
Program:-
read n
printf “\n”
for((i=1;i<=$n;i++))
do
for((j=1;j<=$i;j++))
do
printf “$j”
done
printf “\n”
done
Output:-
4
1
12
123
1234
Example -4 Write a shell script to find the largest among the 3 given numbers.
Program:-
echo “Enter first number: ”
read a
echo “Enter second number: ”
read b
echo “Enter third number: ”
read c
if [ $a -ge $b -a $a -ge $c ]
then
echo “$a is largest integer”
elif [ $b -ge $a -a $b -ge $c ]
then
echo “$b is largest integer”
elif [ $c -ge $a -a $c -ge $b ]
then
echo “$c is largest integer”
fi
Output:-
22
Enter second number:
33
Enter third number:
42
44 is largest integer
Example -5 Write a shell script to find factorial of given number n.
Program:-
fact=1
echo “Enter number to find factorial : ”
read n
a=$n
#if enter value less than 0
if [ $n -le 0 ]
then
echo “invalid number”
exit
fi
#factorial logic
while [ $n -ge 1 ]
do
fact=`expr $fact \* $n`
n=`expr $n – 1`
done
echo “Factorial for $a is $fact”
Output:-
5
Factorial for 5 is 120
Example -6 Write a shell script to print all prime numbers from 1 to n.
Program:-
echo “enter the range”
read n
echo “the prime no are:”
m=2
while [ $m -le $n ]
do
i=2
flag=0
while [ $i -le `expr $m / 2` ]
do
if [ `expr $m % $i` -eq 0 ]
then
flag=1
break
fi
i=`expr $i + 1`
done
if [ $flag -eq 0 ]
then
echo $m
fi
m=`expr $m + 1`
done
Output:-
10
the prime no are
2
3
5
7
Example -7 Write a shell script to reverse a number supplied by a user.
Program:-
then
if [ $1 -gt 0 ]
then
num=$1
sumi=0
while [ $num -ne 0 ]
do
lnum=`expr $num % 10`
sumi=`expr $sumi \* 10 + $lnum`
num=`expr $num / 10`
done
echo “Reverse of digits is $sumi of $1″
else
echo ” Number is less than 0″
fi
else
echo “Insert only one parameter ”
fi
Output:-
Reverse of digits is 321 of 123
Example -8 Write a shell script to find first n Fibonacci numbers like: 0 1, 1, 2, 3, 5, 13,
Program:-
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:-
5
Fibonacci Series up to 5 terms :
0
1
1
2
3
Example -9 Write a shell script to check whether the given number is Perfect or not.
Program:-
read no
i=1
ans=0
while [ $i -le `expr $no / 2` ]
do
if [ `expr $no % $i` -eq 0 ]
then
ans=`expr $ans + $i`
fi
i=`expr $i + 1`
done
if [ $no -eq $ans ]
then
echo $no is perfect
else
echo $no is NOT perfect
fi
Output:-
6
6 is perfect
Enter a number
10
10 is NOT perfect
Example -10 Write a shell script which displays a list of all files in the current directory to which you have read, write and execute permissions
Program:-
do
if [ -r $File -a -w $File -a -x $File ]
then
echo $File
fi
done
Output:-
Documents
Downloads
lab
Music
Pictures
Public
shellscript
Templates
Videos
Example -11 Write a shell script that deletes all the files in the current directory which are 0 bytes in length.
Program:-
find . -name “*” -size -1k –delete
echo “files deleted”
Output:-
Example -12 Write a shell script to check whether the given string is Palindrome or not.
Program:-
echo “Enter the string:”
read str
echo
len=`echo $str | wc -c`
len=`expr $len – 1`
i=1
j=`expr $len / 2`
while test $i -le $j
do
k=`echo $str | cut -c $i`
l=`echo $str | cut -c $len`
if test $k != $l
then
echo “String is not palindrome”
exit
fi
i=`expr $i + 1`
len=`expr $len – 1`
done
echo “String is palindrome”
Output:-
abba
String is palindrome
Enter the string:
abc
String is not palindrome
Example -13 Write a shell script to display the digits which are in odd position in a given 5 digit number
Program:-
read num
n=1
while [ $n -le 5 ]
do
a=`echo $num | cut -c $n`
echo $a
n=`expr $n + 2`
done
Output:-
12345
1
3
5
Example -14 Write a shell script to check given year is leap year or not.
Program:-
echo “enter any year”
read num
if [ `expr $num % 4` -eq 0 ]
then
if [ `expr $num % 100` -eq 0 -a `expr $num % 400` -ne 0 ]
then
echo “Not a leap year”
else
echo “Leap year ”
fi
else
echo “Not a leap year”
fi
Output:-
2016
Leap year
enter any year
2100
Not a leap year
Example -15 Write a shell script to find the value of one number raised to the power of another.
Program:-
read no
echo “Input power”
read power
counter=0
ans=1
while [ $power -ne $counter ]
do
ans=`echo $ans \* $no | bc`
counter=`echo $counter + 1 | bc`
done
echo “$no power of $power is $ans”
Output :-
5
Input power
3
5 power of 3 is 125