Top Most Shell Scripting Interview Questions
Q – 1 How do you write a while loop in shell?
Ans- while {condition} do {statement} done
Q – 2 How does a case statement look in shell scripts?
Ans- case {variable} in {possible-value-1}) {statement};; {possible-value-2}) {statement};; esac
Q – 3 How do you read keyboard input in shell scripts?
Ans- read {variable-name}
Q – 4 How do you define a function in a shell script?
Ans- function-name() { #some code here return }
Q – 5 How do you stop all the processes, except the shell window?
Ans- kill 0
Q – 6 How do you find out about all running processes?
Ans- ps -ag
Q – 7 How do you stop a process?
Ans- kill pid
Q – 8 What’s the command to find out today’s date?
Ans- date
Q – 9 What’s the command to find out users on the system?
Ans- who
Q – 10 How do you find out the current directory you’re in?
Ans- pwd
Q – 11 How do you write a for loop in shell?
Ans- for {variable name} in {list} do {statement} done
Q – 12 What’s a way to do multilevel if-else’s in shell scripting?
Ans- if {condition} then {statement} elif {condition} {statement} fi
Q – 13 How do you find out the number of arguments passed to the shell script?
Ans- $#
Q – 14 How do you do Boolean logic operators in shell scripting?
Ans- ! tests for logical not, -a tests for logical and, and -o tests for logical or.
Q – 15 How do you test for file properties in shell scripts?
Ans- -s filename tells you if the file is not empty, -f filename tells you whether the argument is a file, and not a directory, -d filename tests if the argument is a directory, and not a file, -w filename tests for writeability, -r filename tests for readability, -x filename tests for executability
Q – 16 How do you do number comparison in shell scripts?
Ans- -eq, -ne, -lt, -le, -gt, -ge
Q – 17 What’s the conditional statement in shell scripting?
Ans- if {condition} then … fi
Q – 18 How do you refer to the arguments passed to a shell script?
Ans- , and so on. {xtypo_info}$1, $2 and so on. $0 is your script name.{/xtypo_info} is your script name.
Q – 19 How do you fire a process in the background?
Ans- ./process-name &
Q – 20 How do you find out what’s your shell?
Ans- echo $SHELL
Q – 21 How do you list currently running process?
Ans- ps
Q – 22 How do you search for a string in a directory with the subdirectories recursed?
Ans- grep -r string *
Q – 23 How do you search for a string inside a directory?
Ans- grep string *
Q – 24 How do you search for a string inside a given file?
Ans- grep string filename
Q – 25 How do you count words, lines and characters in a file?
Ans- wc
Q – 26 How do you find out your own username?
Ans- whoami
Q – 27 How do you remove recursively?
Ans- rm -rf
Q – 28 How do you remove a file?
Ans- rm
Q – 29 How do you send a mail message to somebody?
Ans- mail [email protected] -s ‘Your subject’ -c ‘ [email protected]‘
Q – 30 What are PIDs?
Ans- They are process IDs given to processes. A PID can vary from 0 to 65535.