Operating System Long Questions and AnswersHere 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:-

echo “enter command name”

read cmd

$cmd

 

 

Output:-

enter command name

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:-

i) using if..elif statement

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:-

Enter one no.

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:-

echo “enter the number”

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:-

enter the number

4

1

12

123

1234

 

 

Example -4 Write a shell script to find the largest among the 3 given numbers.

 

Program:-

clear

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:-

Enter first number:

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:-

clear

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:-

Enter number to find factorial :

5

Factorial for 5 is 120

 

 

Example -6 Write a shell script to print all prime numbers from 1 to n.

 

Program:-

clear

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:-

enter the range

10

the prime no are

2

3

5

7

 



 
Example -7 Write a shell script to reverse a number supplied by a user.

 

Program:-

if [ $# -eq 1 ]

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:-

bash pr81.sh 123

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:-

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 numbers of terms to be generated?

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:-

echo Enter a number

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:-

Enter a number

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:-

for File in *

do

if [ -r $File -a -w $File -a -x $File ]

then

echo $File

fi

done

 

 

Output:-

Desktop

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:-

clear

find . -name “*” -size -1k –delete

echo “files deleted”

 

 

Output:-

files deleted

 

 

Example -12 Write a shell script to check whether the given string is Palindrome or not.

 

Program:-

clear

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:-

Enter the string:

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:-

echo “Enter a 5 digit number”

read num

n=1

while [ $n -le 5 ]

do

a=`echo $num | cut -c $n`

echo $a

n=`expr $n + 2`

done

 

 

Output:-

Enter a 5 digit number

12345

1

3

5

 

 

Example -14 Write a shell script to check given year is leap year or not.

 

Program:-

clear

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:-

enter any year

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:-

echo “Input number”

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 :-

Input number

5

Input power

3

5 power of 3 is 125

 
You may be interested in:
Operating System Short Descriptive Questions and Answers

Operating System Multiple Choice Questions Answers (MCQs)

Operating System Online Tests

Windows Operating System Online Tests

Linux Operating System Online Tests

Operating System Tutorials