The following Unix Shell Script MCQs have been compiled by our experts through research, in order to test your knowledge of the subject of Unix Shell Script. We encourage you to answer these 50+ multiple-choice questions to assess your proficiency.
Please continue by scrolling down.
A. <>
B. {}
C. ( )
D. &&
E. %
F. 3-5
A. An alphabet
B. An underscore
C. A number and an underscore
D. An alphabet and an underscore
E. A number and a special symbol other than the underscore
What is the error in the following shell script code?
1. until=1
2. while [ $until -eq 5 ]
3. do
4. echo until cannot be used as a variable name
5. until=`expr $until + 1`
6. done
A.
Line 1 should be until=$1
B.
Until cannot be used as a variable name
C.
There is no errorÂ
D.
Line 4 should be echo
E.
Line 4 should be enclosed within { }Â
What is the output of the following program?
b= [ -n $b ]
echo $?
[ -z $b ]
echo $?
A.
1 1
B.
2 2
C.
0 0
D.
0 1
E.
An error will occur
What is the output of the following program?
a=300
[ -n $a ]
echo $?
[ -z $a ]
echo $?
A.
1 2
B.
0 2
C.
2 3
D.
0 0
E.
0 1
What is the error in the following program?
1. x=10
2. if [ x -ge 2 ]
3. then
4. echo $x
5. fi
A.
There is no error
B.
Line 4 should be echo x
C.
Test should be used on line 2 instead of []
D.
Line 1 should be x={10}
E.
Line 2 should be if [ $x -ge 2 ]
A. The condition is tested at the end.
B. It is executed as long as the given condition or list of items evaluate to true.
C. It cannot test for conditions.
D. We cannot use exit statement under until loop.
If x=11 and y=6, then what is the exit status of the following expression?
[ ! $x -gt 9 -a ! $y -ne 23 ]
A.
0Â
B.
1Â
C.
2
D.
3
E.
4
What is the output of the following program?
z='[$x -lt 10 ]'
echo x=$x y=$y z=$z
x=3
y=
A.
x=3 y=1 z=1
B.
x=3 y=0 z=0
C.
x=3 y=3 z=5
D.
x=3 y=2 z=1
E.
None of the aboveÂ
A. Output of echo statement can be redirected to a file.
B. Shell variables are case sensitive.
C. Programs written for Bourne shell are compatible with Korn shell.
D. expr can handle only integers.
E. Comments can be split over multiple lines if each line is preceded by #.
What is the output of the following shell script code?
suite=3
case $suite in
1) echo Diamond ;;
2) echo Spade ;;
3) echo Heart ;;
4) echo Club ;;
A.
Diamond
B.
Spade
C.
HeartÂ
D.
Club
E.
An error will occur
A. if [ -f xyz ]
B. if [ -d xyz ]
C. if [ -v xyz ]
D. if [ -r xyz ]
E. if [ -e xyz ]
A. It is a decision making construct.
B. The group of commands between the then and else forms the if block.
C. The else block is optional.
D. The indentation is done in the if-then-fi to improve readability.
E. The indentation is mandatory otherwise an error will occur on execution.
If x=11 and y=6, then what is the exit status of the following expression?
[ $x -eq 11 -a $y -ne 89 ]
A.
0Â
B.
1
C.
2
D.
3
E.
4
A. a=expr $b + $c
B. a=`expr $b * $c
C. a=`expr $b * ( $c + $d )`
D. a=expr $c * $d
E. a=`expr $b \* \( $c + $d \ )
A. An error will occur
B. dog
C. cuckoo
D. cuckoo parrot dog
E. dog parrot cuckoo
A. It cannot be used inside a loop.
B. It continues the control to the beginning of the loop, bypassing the statements below it, inside the loop.
C. It can only be used in a while loop.
D. It has to be associated with an if statement.
E. It is used with the case construct.
Which of the following lines have an error?
1. echo enter your name
2. read filename
3. read name < $filename
4. echo $name
A.
1
B.
2Â
C.
3Â
D.
4
E.
There is no error.Â
What is the error in the following program?
1. while who | grep aa12 | wc -l
2. do
3. echo false
4. done
A.
There is no error
B.
Condition on line 1 is invalid
C.
Line 3 should be echo
D.
Line 3 should be enclosed with {}
E.
Line 3 should be echo {false}
A. a='cat file'
B. b=100
C. c=50
D. d = 25
E. e='ls'
What is the output of the following program?
i=1
for [ i -le 10 ]
do
echo $i
i=
A.
An error will occur
B.
1
C.
2
D.
5
E.
10
The break statement is used to exit from:
A.
the script.
B.
a
C.
an
D.
the shell.
E.
the
What is the error in the following program?
1. j=10 k=12
2. if [ $k>=$j ]
3. then
4. k=$j
5. j=$k
6. fi
7. echo $j $k
A.
One cannot declare two variables on line 1
B.
Statements on lines 4 and 5 must appear with curly braces {}
C.
On line 2, test should be used instead of []
D.
There is no error
E.
On line 2 -ge should be used instead of >=
A. is used last.
B. is used first.
C. is unary type i.e. the operators that operate on single operand.
D. is binary type i.e. the operators that operate on two operands.
What is the output of the following program?
i=1 j=1 k=1 while [ $i -lt 10 ] do while [ $j -lt 10 ] do while [ $k -lt 10 ] do echo $i $j $k k='expr $k + 1' break 3 done j='expr $j + 1' done i='expr $i + 1'
A. An error will occur
B. 3 4 5
C. 2 2 2
D. 2 3 4
E. 1 1 1
A. exit from the
B. exit from the
C. terminate the execution of a script.
D. exit from an
A. 1
B. -1
C. 2
D. -2
What is the error in the following shell script code?
#!/bin/sh
1. a=12.25 b=12.52
2. if [a=b]
3. then
4. echo "\na and b are equal"
5. fi
A.
There is no error
B.
Variable declaration on line 1 is incorrect
C.
The statement to be printed with echo on line 4 should be within curly brackets
D.
fi should be replaced by endif on line 5
E.
On line 2, [a=b] should be replaced with [ $a -eq $b ]Â Â
What is the output of the following program?
i=4 z=12
[ $i = 5 -a $z -gt 5 ]
A.
0
B.
1Â
C.
2
D.
3
E.
4
What is the output of the following program?
terminal=vt100
case $terminal in
vt100) echo Dec terminal;;
vt200) echo Old terminal;;
ansi) echo Commonly used terminal;;
v*) echo vt series terminal;;
*) echo Any terminal;;
esac
A.
Dec terminal
B.
Old terminal
C.
commonly used terminal
D.
vt series terminal
E.
Any terminal
A. shift
B. readonly
C. ls
D. unset
E. echo
What is the error in the following loop statement?
while [ $1 -gt 10 -a \($2 -o -w $3 \) ]
A.
There is no errorÂ
B.
( ) brackets are to be used instead of []
C.
{} brackets are to be used instead of []
D.
One cannot use -gt and -o in the same expression
E.
One cannot use -o and -w in the same expression
A. 1
B. 2
C. 2.5
D. 3
A. 1
B. 2
C. -2
D. 0.285
A. _newvar
B. variable
C. #regpay
D. expr
A. a=`ls`
B. b=`ls -l`
C. c=`1972`
D. d=`who`
E. e=`who | grep aa1`
On executing the statement:
set -3 + 1
A.
$1 would be -3
B.
$1 would be 1
C.
$1 would be -
D.
$1 would be set
E.
An error would occur Â
A. While executing a shell script, the shell acts as a compiler.
B. Variables declared in a shell script can be displayed at the dollar prompt using the set command.
C. There is no restriction on the length of a shell variable name.
D. Any shell script by default gets executed in the current shell.
E. A shell variable cannot hold negative values.
A. a code generator.
B. an assembler.
C. an interpreter.
D. a compiler.
A. a=
B. a= ''
C. a=
D. All of the above.
What is the error in the following program?
1. j=1
2. while [ $j -le 10 ]
3. do
4. echo $j
5. j = j + 1
6. done
A.
There is no error
B.
Line 1 should be j=$1
C.
Statements on lines 3 and 4 should be between curly brackets
D.
Line 4 should be echo {$j}
E.
Line 5 should be j=`expr $j + 1`
What is the output of the following program?
for i in a b c d e
do
echo $i
done
A.
5
B.
1 2 3 4 5
C.
a b c d e
D.
e d c b a
E.
An error will occur
F.
a b c d e
What will be the output of the following program assuming that the command line arguments are Unix shell scripting?
for argument in *
do
echo $argument
done
A.
An error will occur
B.
Unix shell scripting
C.
Unix shell scripting
D.
shell
E.
The names of all files in the current directory would be displayedÂ
What is the error in the following program?
1. j=10 k=12
2. if test [ $k -ge $j ]
3. then
4. k=$j
5. j=$k
6. fi
7. echo $j $k
A.
There is no error.
B.
Variable declaration on line 1 is incorrect.
C.
The variables to be outputted on line 7 should be enclosed within quotes.
D.
Output will be 10 12 with a warning message in line 2Â
E.
fi should be replaced by endif on line 6
A. while [ $? -eq 0 ]
B. while [ $count -le 0.1]
C. while [ $1 -gt 10]
D. while [ 0 ]
E. while $
Suppose you are writing a shell script that accepts five positional parameters from the terminal. What will be the effect of using statement "shift 1" in your shell script and then executing the shell script?
A.
The positional parameters would be shifted by one position towards left i.e. parameter 1 will have value of parameter 2, parameter 2 will have value of parameter 3 and so on.Â
B.
Only the first positional parameter would be shifted to left position.
C. All of the positional parameters would be shifted by one position towards right i.e. parameter 2 will have value of parameter 3, parameter 3 will have value of parameter 4 and so on.
D.
It would result in an error.
What is the output of the following program?
1. x=3 y=5 z=10
2. if [ \($x -eq 3\) -a \( $y -eq 5 -o $z -eq 10 \) ]
3. then
4. echo $x
5. else
6. echo $y
7. fi
A.
1 and a warning about line 2 will occur
B.
3 and a warning about line 2 will occur
C.
5 and a warning about line 2 will occur
D.
10 and a warning about line 2 will occur
E.
An error will occur
What is the output of the following program when directory name "home" is given as input?
echo Enter a directory Name
read dirname
case $dirname in
*) echo any directory name ;;
c*) echo cobol directory name ;;
f*) echo fortran directory name ;;
p*) echo pascal directory name ;;
esac
A.
any directory name
B.
cobol directory name
C.
fortran directory name
D.
pascal directory name
E.
An error will occur
What is the output of the following program?
k=35
echo `[ $k -eq 35 ]``[ $k -eq 50 ]`
A.
1 1
B.
0 0
C.
An error will occur
D.
A blank line will result
A. t = 256 (space on both sides of '=' operator)
B. 3.14 * $r * $r = area
C. k=`expr a*b`
D. tput cup $row $col