Unix Shell Scripting Interview Questions and Answers

Ratings:
(4)
Views:396
Banner-Img
  • Share this blog:

Unix Shell Scripting Interview Questions and Answers

1Q) What is UNIX?

Ans: It is a portable operating system that is designed for both efficient multi-tasking and multi-user functions. Its portability allows it to run on different hardware platforms. It was written in C and lets the user do processing and control under a shell.

2Q) What is Shell?

Ans: A shell acts as an interface between the user and the system. As a command interpreter, the shell takes commands and sets them up for execution.

3Q) What is the Zombie process in UNIX? How do you find the Zombie process in UNIX?

Ans: When a program forks and the child finishes before the parent, the kernel still keeps some of its information about the child in case the parent might need it.

for example, the parent may need to check the child’s exit status. To be able to get this information, the parent calls ‘wait()’;

In the interval between the child terminating and the parent calling ‘wait()’, the child is said to be a ‘zombie’ (If you do ‘ps’, the child will have a ‘Z’ in its status field to indicate this.)

4Q) What is nohup in UNIX?

Ans: Nohup is a special command which is used to run the process in the background, but it is slightly different than & which is normally used for putting a process in the background. A UNIX process started with nohup will not stop even if the user who has stared log off from the system. While the background process started with & will stop as soon as the user log off.

5Q) What type of buffer is supported by Unix?

Ans:

  • Fully Buffered.
  • Line Buffered.
  • Un-Buffered.

Interested in mastering Unix Shell Scripting Training? Enroll now for FREE demo on "Unix Shell Scripting Training"

6Q) What are signals?

Ans: Signals are software interrupts. Signal provides a way of handling asynchronous events: a user at a terminal typing the interrupt key to stop a program or the next program in the pipeline terminating prematurely.

7Q) What is the session?

Ans: A Session is a collection of one or more process groups. A process establishes a new session by calling setsid function. This function return process groupid ok.

8Q) What is ‘ps’ command for?

Ans: The ps command prints the process status for some or all of the running processes. The information given are the process identification number (PID),the amount of time that the process has taken to execute so far, etc.

9Q) How are devices represented in UNIX?

Ans: All devices are represented by files called special files that are located in/dev directory. Thus, device files and other files are named and accessed in the same way. A ‘regular file’ is just an ordinary data file in the disk.

10Q) What are the different type of variables used in a Shell Script ?

Ans: In a shell script we can use two types of variables :

  • System-defined variables
  • User-defined variables

System-defined variables are defined or created by Operating System(Linux) itself. These variables are generally defined in Capital Letters and can be viewed by “set” command.

11Q) How to use Argument in a script?

Ans: First argument: $1,

Second argument :

$2 Example :

Script will copy file (arg1) to destination (arg2) ./copy.sh file1.txt /tmp/ cat copy.sh #!/bin/bash cp $1 $2

12Q) How to check if previous command run successful? $? How to check if file exist on filesystem?

Ans: if [ -f /var/log/messages ]

then echo “File exists”

13Q) What does it mean #!/BIN/SH OR #!/BIN/BASH at beginning of every script?

Ans: That line tells which shell to use. #!/bin/bash script to execute using /bin/bash. In the case of python script, there will be #!/usr/bin/python.

14Q) What is the first symbol in the bash script file?

Ans: #

What would be the output of command: `{` -Z “” `}` && ECHO 0 || ECHO 1?  

15Q) What difference between & And &&?

Ans:

  • & – we using it when want to put the script to the background
  • && – when we want to execute command/script if first script was finished successfully.

16Q) What would be the output of the command: ECHO ${NEW:-VARIABLE}?

Ans:

  • Variable

17Q) How to redirect STDOUT and STDERR Streams to LOG.TXT file from the script inside?

Ans: Add “exec >log.txt 2>&1” as the first command in the script.

18Q) How to get HOME_DIR with echo command only if string variable=”USER:123:321:/HOME/DIR” is given?

Ans: echo ${variable#*:*:*:} or

echo ${variable##*:}

19Q) How to list users that UID less than100(awk)?

Ans: awk -F: ‘$3<100’ /etc/passwd

20Q) How to change the standard field separator to“:” in bash shell?

Ans: IFS=”:”

21Q) How to print all arguments provided to the script?

Ans: echo $* or echo $@

22Q) How to define an array in bash?

Ans: array=(“Hi” “my” “name” “is”)

23Q) How will you pass and access arguments to a script in Linux?

Ans: Arguments can be passed as: script name “Arg1” “Arg2”….”Argn” and can be accessed inside the script as $1 , $2 .. $n.

24Q) How do you write the contents of 3 files into a single file?

Ans: cat file1 file2 file3 > file

25Q) Write a command to find the sum of bytes (size of the file) of all files in a directory?

Ans:

ls -l | grep '^-'|

awk 'BEGIN {sum=0}

{sum = sum + $5}

END

{print sum}'

26Q) Write a command to select only those lines containing "july" as a whole word?

Ans:

grep -w july filename The '-w' option makes the grep command to search for exact whole words. If the specified pattern is found in a string, then it is not considered as a whole word.

For example: In the string "mikejulymak", the pattern "july" is found. However "july" is not a whole word in that string.

27Q) Write a command to duplicate each line in a file?

Ans: sed 'p' < filename

28Q) Write a command to list the files in '/usr' directory that start with 'ch' and then display the number of lines in each file?

Ans: wc -l /usr/ch* Another way is find /usr -name 'ch*' -type f -exec wc -l {} ;

29Q) Write a command to remove the prefix of the string ending with '/'?

Ans: The basename utility deletes any prefix ending in /. The usage is mentioned below: basename /usr/local/bin/file This will display the only file.

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox