Top 10 shell scripting interview questions and answers
Top 10 Shell Scripting Interview Questions with Answers
Here you will learn top 10 linux shell scripting interview questions with answers. Please practice these questions as 90% questions will be asked during in linux admin interview.
Q:1 How many types of variables used in a shell Script?
Ans: There are two types of variables used in Shell Scripts:
- System Variables
- User defined Variables
- System variables are defined or created by OS itself. These variables are generally defined in Capital Letters and we can check these variables by %u2018printenv%u2019 command.
SHELL=/bin/bash
TERM=xterm
USER=testuser
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca:%u2026
MAIL=/var/mail/testuser
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
PWD=/home/testuser
LANG=en_US.UTF-8
SHLVL=1
HOME=/home/testuser
LOGNAME=testuser
LESSOPEN=| /usr/bin/lesspipe %s
LESSCLOSE=/usr/bin/lesspipe %s %s
_=/usr/bin/printenv
- User defined variables are created by users and the values of variables can be viewed by using the command %u201Cecho $%u201D.
var1=20
var2=testing
var3=%u201Ctesting not done yet%u201D
Q:2 How to check previous command execution status in shell script ?
Ans: While writing a shell script , if you want to check previous command is executed successfully or not , then we use %u201C echo $?%u201D with if statement to check the exit status of previously executed command.
root@testserver:~# ls /usr/bin/pwd /usr/bin/pwd root@testserver:~# echo $? 0 If exit status is 0 , then command was executed successfully root@testserver:~# ls /usr/bin/linux ls: cannot access /usr/bin/linux: No such file or directory root@testserver:~# echo $? 2 If the exit status is other than 0, then we can say command is not executed successfully.
Q:3 What is the basic syntax of WHILE loop in shell scripting ?
Ans: The while loop repeats its block of commands a number of times. The while loop iterates until its while condition is no longer true. The basic syntax is .
while [ test_condition ]
do
command
done
Q:4 What is the syntax of FOR loop in shell script ?
Ans: Basic Syntax of for loop is given below :
for variable in list_of_items
do
command1
command2
%u2026.
last_command
done
Q:5 How to put comments in your bash shell script?
Ans: Comments are the messages for other users that describe the functionality of a shell script. To put comments in your script, start each comment line with a hash sign (#) as below.
#!/bin/bash
#This is a comment
#The below command show the logged in user
echo %u201CI am $USER%u201D
Q:6 How to get input from the command terminal in a shell script?
Ans: %u2018read%u2019 command reads in data from the command line terminal . The read command takes input whatever the user types and places the text into the variable you name as shown below.
# vim /tmp/test.sh #!/bin/bash echo %u2018Please enter your name%u2019 read name echo %u201CMy Name is $name%u201D #./test.sh Please enter your name Ravindra My Name is Ravindra
Q:7 What is the syntax of IF ELSE Conditionals in shell script?
Ans: The if..else statement is the control statement that allows shell to make correct decision out of two conditions. The syntax as shown below.
if [ expression ]
then
Statement(s) to be executed if expression is true
else
Statement(s) to be executed if expression is not true
fi
Q:8. What is Shebang in shell script?
Ans: Shebang is a # sign followed by an exclamation i.e. !. Generally, this can be seen at the top of the script. Usually, a developer or administrator uses this to avoid repetitive work. Shebang determines the location of the engine which is to be used in order to execute the script.
Here %u2018#%u2019 symbol is called hash and %u2018!%u2019 is called a bang.
Instance: #!/bin/bash
The above line tells which shell to use.
Q:9 What is the significance of $# ?
Ans: $# shows the count of the arguments passed to a script.
Q:10 What is the difference between $* and $@ ?
Ans: $@ considers each quoted arguments as separate arguments whereas $* will consider the entire set of positional parameters as single string.
Hence, you have gone through top 10 linux shell scripting interview questions . I hope you are feeling confident.
Comments
Post a Comment