Shell Script Interview Questions

Shell Script Interview Questions
Question: How do you find out what’s your shell? - echo $SHELL
Question: What’s the command to find out today’s date? - date
Question: What’s the command to find out users on the system? - who
Question: How do you find out the current directory you’re in? - pwd
Question: How do you remove a file? - rm
Question: How do you remove a - rm -rf
Question: How do you find out your own username? - whoami
Question: How do you send a mail message to somebody? - mail somebody@cinterviews.com -s ‘Your subject’ -c ‘cc@cinterviews.com
Question: How do you count words, lines and characters in a file? - wc
Question: How do you search for a string inside a given file? - grep string filename
Question: How do you search for a string inside a directory? - grep string *
Question: How do you search for a string in a directory with the subdirectories recursed? - grep -r string *
Question: What are PIDs? - They are process IDs given to processes. A PID can vary from 0 to 65535.
Question: How do you list currently running process? - ps
Question: How do you stop a process? - kill pid
Question: How do you find out about all running processes? - ps -ag
Question: How do you stop all the processes, except the shell window? - kill 0
Question: How do you fire a process in the background? - ./process-name &
Question: How do you refer to the arguments passed to a shell script? - $1, $2 and so on. $0 is your script name.
Question: What’s the conditional statement in shell scripting? - if {condition} then … fi
Question: How do you do number comparison in shell scripts? - -eq, -ne, -lt, -le, -gt, -ge
Question: How do you test for file properties in shell scripts? - -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
Question: How do you do Boolean logic operators in shell scripting? - ! tests for logical not, -a tests for logical and, and -o tests for logical or.
Question: How do you find out the number of arguments passed to the shell script? - $#
Question: What’s a way to do multilevel if-else’s in shell scripting? - if {condition} then {statement} elif {condition} {statement} fi
Question: How do you write a for loop in shell? - for {variable name} in {list} do {statement} done
Question: How do you write a while loop in shell? - while {condition} do {statement} done
Question: How does a case statement look in shell scripts? - case {variable} in {possible-value-1}) {statement};; {possible-value-2}) {statement};; esac
Question: How do you read keyboard input in shell scripts? - read {variable-name}
Question: How do you define a function in a shell script? - function-name() { #some code here return }
Question: How does getopts command work? - The parameters to your script can be passed as -n 15 -x 20. Inside the script, you can iterate through the getopts array as while getopts n:x option, and the variable $option contains the value of the entered option.

No comments: