Please find below list of Shell Script Questions and Answers.

Shell Script Questions and Answers

1. Given two integers,  X and Y , find their sum, difference, product, and quotient.

Constraints

X >= -100

Y <= 100 and Y != 100

read X
case ${X#[-+]} in
  *[!0-9]* | '') echo "Not an integer" ;;
esac

read Y
case ${Y#[-+]} in
  *[!0-9]* | '') echo "Not an integer" ;;
esac

if [[ $X -ge -100 && $Y -le 100 && $Y -ne 0 ]]
then
   echo `expr $X + $Y`
   echo `expr $X - $Y`
   echo `expr $X \* $Y`
   echo `expr $X / $Y`
fi 

2. Given two integers, X and Y , identify whether  or  or .

Exactly one of the following lines:
– X is less than Y
– X is greater than Y
– X is equal to Y

read X
case ${X#[-+]} in
  *[!0-9]* | '') echo "Not an integer" ;;
esac

read Y
case ${Y#[-+]} in
  *[!0-9]* | '') echo "Not an integer" ;;
esac
if [[ $X -gt $Y ]]
then
  echo "X is greater than Y" 
elif [[ $X -lt $Y ]]
then
  echo "X is less than Y"
else
  echo "X is equal to Y"
fi

3. Read in one character from STDIN.
If the character is ‘Y’ or ‘y’ display “YES”.
If the character is ‘N’ or ‘n’ display “NO”.
No other character will be provided as input.

Input Format

One character

Constraints

The character will be from the set . {yYnN}

Output Format

echo YES or NO to STDOUT.

read answer
case ${answer:0:1} in
    y|Y )
        echo "YES"
    ;;
    n|N )
        echo "NO"
    ;;
esac

Given three integers (X,Y , and Z) representing the three sides of a triangle, identify whether the triangle is scalene, isosceles, or equilateral.

  • If all three sides are equal, output EQUILATERAL.
  • Otherwise, if any two sides are equal, output ISOSCELES.
  • Otherwise, output SCALENE.

Input Format

Three integers, each on a new line.

read X
read Y
read Z

if [[ $X -eq $Y && $X -eq $Z ]]
then
  echo "EQUILATERAL"
elif [[ ($X -eq $Y && $Y -ne $Z) || ($Y -eq $Z && $Y -ne $Z) || ($X -eq $Z && $Y -ne $Z )]]
then
  echo "ISOSCELES"
else 
  echo "SCALENE"
fi
Shell Script Questions and Answers
(Visited 380 times, 26 visits today)