Monday 5 August 2019

Must read unix/Linux shell scripting interview questions

Unix is used everywhere and knowledge of the operating system is must for any interview. Here are the must read  unix shell scripting interview questions

1) What is Shell Script?
Answer
A shell script is a collection of command which are executed in a order given. There are conditional statement and looping also available like if ,while which helps in finding if a particular value is greater than another value .
To write any comments in the shell scripts ,it has to be written with # preceded
2) What are different type of shell available?
Answer
ShellDescription
Bourne shellThis is the original Unix shell written by Steve Bourne of Bell Labs. It is available on all UNIX systems.
Below is written  in the beginning of shell scripts to specify the shell to use for the scripts. The default prompt on the Unix for this is $
#!/bin/bsh
C shellThis shell was written at the University of California, Berkeley. It provides a C-like language with which to write shell scripts – hence its name.The default prompt on the Unix for this is %
Below is written  in the beginning of shell scripts to specify the shell to use for the scripts
#!/bin/csh
Korn shellThis shell was written by David Korn of Bell labs. It is now provided as the standard shell on Unix systems. It provides all the features of the C shell with a shell programming language similar to that of the original Bourne shell. It is the most efficient shell. Consider using this as your standard interactive shell.The default prompt on the Unix for this is $Below  is written in the beginning of shell scripts to specify the shell to use for the scripts
#!/bin/ksh
3) How will you find which operating system your system is running on in UNIX?
Answer
By using command “uname -a” in UNIX
4) How do you find which processes are using a particular file?
Answer
By using fuser command in UNIX. It wills list down PID of all the process which is using a particular file.
unix shell scripting interview questions
5) What is the use of “$?” sign in shell script ?
Answer
This is used to find the status of last executed command
unix shell scripting interview questions
6) How to make a shell script executable ?
Answer
Using the chmod command we can make a shell script executable.
This chmod command makes the shell script file “test.sh” executable for the user (owner) only:
$ chmod u+x test.sh
this syntax makes it executable for all (everyone):
$ chmod a+x test.sh

7) How to put comments in your shell script ?
Answer
Comments are the messages to yourself and for other users that describe what a script is supposed to do and how its works.It also help in understanding the script in future. To put comments in your script, start each comment line with a hash sign (#) . Example is shown below :
#!/bin/ksh
# This scripts  is created by techgoeasy.com
echo “$ORACLE_HOME” #!/bin/ksh
# This scripts  is created by techgoeasy.com
echo “$ORACLE_HOME”
8) How do you find the uptime of the Unix box?
Answer
By using uptime command in UNIX
or who -r

9) How do you find which process is taking how much CPU?
Answer
By using “top” command in UNIX
10)  How to find the last 10 lines of the files
Answer
tail -10 <file name>
11)  How to count the occurence of the word “oracle” in the file loader.txt?
Answer
grep -c “oracle” loader.txt
12)  How do you find which remote hosts are connecting to your host on a particular port say 8000?
Answer
By using netstat command execute netstat -an | grep “port”
netstat -an|grep 8000

13) You have a comma separated file which contains seqno, Name and Address, We have to write command to list down all names without seqno and Addresses?
Answer
you can either you AWK or CUT command here.
cut -d”;”  -d2  filename
or
cat filename|awk -F”;” {print $2}’
14) What is the use of break  and continue command ?
Answer
The break command is a simple way to escape out of a loop in progress. We can use the break command to exit out from any loop, including while and until loops.
The continue command is identical to break command except it causes the present iteration of the loop to exit, instead of the entire loop. Continue command is useful in some scenarios where error has occurred but we still want to execute the next commands of the loop.
15)  What is the Syntax of “Case statement” in Linux shell scripting ?
Answer
The basic syntax is shown below :
case word in
type1)
Statement(s) to be executed if type1 matches
;;
type2)
Statement(s) to be executed if type2 matches
;;
type3)
Statement(s) to be executed if type3 matches
;;
esac
16) What is the syntax of for loop in shell script ?
Ans: Basic Syntax of for loop is given below :
for variables in list_of_items
do
command1
command2
….
last_command
done
17) How to debug the shell scripts?
Answer
We can debug shell script by running that with option -x
ksh -x test.sh

18)
How to test files in a shell script ?
Answer
command is used to perform different test on the files. Basic test are listed below :
Usage
-d filename
Returns true if the file exists and is a directory
-e filename
Returns true if the file exists
-f filename
Returns true if the file exists and is a regular file
-r filename
Returns true if the file exists and have read permissions
-s filename
Returns true if the file exists and is not empty
-w filename
Returns true if the file exists and have write permissions
-x filename
Returns true if the file exists and have execute permission

19) How do you find the IP address of the Unix machine or if the IP address is given ,the name of the machine
Answer
We can use nslookup command for that
20)What are inodes?
Anwser
it ’ is a ‘data-structure’, which is used for file identification on Linux. Each file on an Unix System has a separate ‘inode’ and an ‘Unique’ inode Number.

21) How do you check if a particular process is listening on a particular port on remote host?
Answer
By using telnet command for example “telnet hostname port”, if it able to successfully connect then some process is listening on that port.
22)  How to list the files in the directory?
Answer
By using the command ls . we have various option available there
23) How to find the filesystem size,user space, free space?
Answer
df -h .
or
df -k .
24) How to perform arithmetic operation ?
Answer
two ways to perform arithmetic operations :
1. Using expr command (# expr 1+ 2 )
2. using a dollar sign and square brackets ( $[ operation ] ) Example : test=$[1 + 4] ; test=$[1 + 4]
25)  How to search for the file in Unix operating system?
Answer
We can use find command for that
find . -name “<filename>”  -print
26)  How to use awk command ?
Answer
27) How to unset or de-assign variables ?
Answer
‘unset’ command is used to  unset a variable. Syntax is shown below :
# unset <Name_of_Variable>
28) What is nohup in UNIX?
Answer
nohup is a special command which is used to run process in background, but it is slightly different than & which is normally used for putting a process in background. An UNIX process started with nohup will not stop even if the user who has stared log off from system. While background process started with & will stop as soon as user logoff.
29) How do you set environment variable which will be accessible form sub shell?
Answer
By using export command, for example export var=x will be available on all sub shell.
30) How do you copy file on the same host or on the remote host?
Answer
we can use cp command for the same host
rcp or scp is used for copying on the remote host

0 comments:

Post a Comment