Saturday, March 30, 2019


Decision Making Statements: -

Q) What are decision making statements ?
Ans – There are two decision making statements. Such as –
1.       If else fi
2.       case esac

Q) Write a shell script whether a given number will be even or odd ?
Ans –
#! /bin/sh
echo “Enter a number: “
read n
if [ `expr $n % 2` -eq 0 ]
then
echo “$n is even number”
else
echo “$n is odd number”
fi

Q) Take two numbers from command line and check which is greater ?
Ans –
$ vi script7.sh
#! /bin/sh
If [ $1 –gt $2 ]
then
echo “$1 is greater than $2”
else
echo “$2 is greater than $1”
fi
ESC + :wq<Enter – Key>
$ chmod 755 script7.sh
$ ./script7.sh 23 45<Enter – Key>
Output –
45 is greater than 23

Q) Take two numbers as user input. Get the summation of it, if sum is greater than 10, then multiply it with 2 else multiply it with 3 and display the result ?
Ans –
#! /bin/sh
echo “Enter two numbers: “
read a b
s=`expr $a + $b`
if [ $s –gt 10 ]
then
echo “Result = `expr $s \* 2`”
else
echo “Result = `expr $s \* 3`”
fi

Q) Write a script to swap two numbers without help of third variable ?
Ans –
#! /bin/sh
echo “Enter two numbers: “
read a b
echo “Before swapping a = $a and b = $b”
a=`expr $a + $b`
b=`expr $a - $b`
a=`expr $a - $b`
echo “After swapping a = $a and b = $b”

Q) Write a script to swap two numbers with help of third variable ?
Ans - 
#! /bin/sh

echo “Enter two numbers: “
read a b
echo “Before swapping a = $a and b = $b”

c=$a
$a=$b
$b=$c
echo “After swapping a = $a and b = $b”

Q) What are syntax of different if else fi statements ?
Ans –
Simple IF:
If [ Condition ]
then
statements
fi
IF ELSE FI:
If [ Condition ]
then
statements
else
statements
fi
Nested IF:
If [ Condition ]
then
statements
                     If [ Condition ]
                     then
                     statements
                     else
                     statements
                     fi
else
statements
                     If [ Condition ]
                     then
                     statements
                     else
                     statements
                     fi
fi
ELSE IF LADDER:
If [ Condition ]
then
statements
elif [ Condition ]
then
statements
elif [ Condition ]
then
statements
else
statements
fi

Q) What is the syntax of case esac statements ?
Ans –
case variable in
Value1)
echo “Statements”
;;
Value2)
echo “statements”
;;
0)
exit
;;
esac

Q) Write a script to know case esac statements ?
Ans –
#! /bin/sh
echo “Enter the state name or zero to exit: “
read state
case $state in
“Odisha”)
echo “Bhubaneswar”
;;
“Karnataka”)
echo “Bengaluru”
;;
0)
exit
;;
esac

Q) What are shell loops ?
Ans –
1.       while
2.       until
3.       for
4.       select

Q) What is the syntax of while loops ?
Ans –
while [ Condition ]
do
statements
done


Part – 1: -
Q) Write a script to print 1 to 10 ?
Ans –
#! /bin/sh
i=1
while [ $i –le 10 ]
do
echo “$i”
i=`expr $i + 1`
done

Q) Write a script to print all even numbers between 50 to 100 ?
Ans –
#! /bin/sh
i=50
while [ $i –le 100 ]
do
echo “$i”
i=`expr $i + 2`
done

Q) Print first 10 even numbers ?
Ans –
#! /bin/sh
i=1
count = 1
while [ $count –le 10 ]
do
echo “`expr $i \* 2`”
count=`expr $count + 1`
i=`expr $i + 1`
done

Q) Write a script to know whether a number is prime or not ?
Ans –
echo “Enter a number: “
read num
i=1
count=0
while [ $i –le $num ]
do
if [ `expr $num % $i` -eq 0 ]
then
count=`expr $count + 1`
fi
i=`expr $i + 1`
done
if [ $count –eq 2 ]
then
echo “$num is a prime number”
else
echo “$num is not a prime number”
fi

Q) Check whether a number is palindrome or not ?
Ans –
#! /bin/sh
echo “Enter a number: “
read num
i=0
j=$num
while [ $j –gt 0 ]
do
rem=`expr $j % 10`
i=`expr $i \* 10 + $rem`
j=`expr $j / 10`
done
if [ $i –eq $num ]
then
echo “$num is a palindrome number”
else
echo “$num is not a palindrome number”
fi

No comments:

Post a Comment

Interview Questions: - (UNIX - 0) UNIX: -   Q) How to get inode number of a file/directory? Ans – $ ls –i <File/Directory_Name...