Shell Scripting Interview Questions & Answers |
Shell Scripting Interview Questions & Answers
Q:1 Explain the default login shell and how to change the default login shell
for a specific user?
Ans: Linux Operating system “/bin/bash” is the default login shell that is
assigned while user creation. To change the default shell using the “chsh” command.
Example:
# chsh <username> -s <new_default_shell>
# chsh linuxorg -s /bin/sh
Q:2 Explain the different types of variables used in a Shell Script?
Ans: Linux shell script we can use two types of variables :
(a)System defined variables
(b)User-defined variables
System-defined variables are defined or created by Operating System itself.
Variables are normally defined in Capital Letters and can be viewed by the “set” command.
To display the value of system-defined variables uses the echo command.
examples are echo $PWD or echo $HOME
User-defined variables are created by system users and the values of variables
can be viewed by using the command “echo $<Variable_Name>”
Q:3 How do you redirect both standard output and standard error to the same location?
Ans: There are two methods to redirect std output and std error to the same location.
These methods are listed below:
First Method 2>&1 # ls /usr/share/doc > out.txt 2>&1
Second Method &> # ls /usr/share/doc &> out.txt
Q:4 Can you write the Syntax of “nested if statement” in shell scripting?
Ans: Basic Syntax is shown below :
if [ Condition ]
then
command1
command2
…..
else
if [ condition ]
then
command1
command2
….
else
command1
command2
…..
fi
fi
Q:5 What is the “$?” sign in the shell script?
Ans: In a shell script, if you check whether the previous command is executed
successfully or not, then we can use “$?”.This command is used to print the
value of the ‘$?’ variable. This variable is generally used in the if statement
to check the exit status of the previous command.
The basic example is shown below :
root@localhost:~# ls /usr/bin/shar
/usr/bin/shar
root@localhost:~# echo $?
0
If the exit status is 0, then the command is executed successfully
root@localhost:~# ls /usr/bin/share
Output:
ls You cannot access /usr/bin/share:
root@localhost:~# echo $?
2
If the exit status is other than 0, then we can say the command is not executed successfully.
Q:6 How do you compare numbers in Linux shell Scripting?
Ans: We can compare the numbers in the shell script by using parameters like ‘-gt’
(greater than), ‘-eq’ (equals to), and ‘-lt’ (less than) in the if statement.
An example is shown below :
#!/bin/bash
x=50
y=40
if [ $x -gt $y ]
then
echo “x is greater than y”
else
echo “y is greater than x”
fi
Q:7 What is the break command?
Ans: The break command is used to escape out of a loop in progress. The break
the command is used to exit out from any loop, including while and until loops.
Q:8 What is the continue command in shell scripting?
Ans Continue command is similar to the break command except it uses the current
iteration of the loop to exit, instead of the entire loop. Continue command
is useful in some structures where an error has occurred but we still want
to execute the next commands of the loop.
Q:9 Explain the Syntax of “Case statement” in Linux shell scripting?
Ans: The basic syntax is shown below :
case word in
value1
command1
command2
…..
last_command
!!
value2
command1
command2
……
last_command
;;
esac
Q:10 What is the syntax of while loop in shell scripting?
Ans: It is similar to for loop, the while loop repeats its block of commands a number of times. Unlike the for loop, however, the while loop iterates until its while condition are no longer true. The basic syntax is :
while [ test_condition ]
do
commands…
done
Q:11 How do you make a shell script executable?
Ans: By the chmod command we can make a shell script executable. An example is shown below :
# chmod a+x myscript.sh
Q:12 What is “#!/bin/bash”?
Ans: #!/bin/bash is known as shebang, where # symbol is called hash and ‘!’ is called a bang. This command is to be executed via /bin/bash.
Q:13 Write the syntax of for loop in a shell script?
Ans: Syntax of for loop are Following:
for variables in list_of_items
do
command1
command2
….
last_command
done
Q:14 How do you debug a shell script?
Ans: The shell script can be debugged by executing the script with the ‘-x’ option
( sh -x myscript. sh). one more way to debug a shell script is by using the ‘-nv’
option ( sh -nv myscript. sh).
Q:15 How do you compare the strings in the shell script?
Ans: By using the test command is used to compare the text strings. This command is used to
compare text strings by comparing each character in each string.