Top 36 Shell Scripting Interview Questions

Top 36 Shell Scripting Interview Questions: Here are the top interview questions of Shell Scripting. Be prepared for the interview. You can download the entire questions in a pdf format. so guys All the best for your interview.

1) What is shell script?

A shell script is a command containing text file that contains one or more commands.


2) Why is the use of shell script?

It is used by system administrators to issue numbers of commands to accomplish the task. All commands are added together in a text file (shell script) to complete daily routine tasks.


3) What are the advantages of shell scripting?

These are the two main advantages of shell scripting:

  • It facilitates you to develop your own operating system with relevant features best suited for you.
  • You can design software applications according to their platform.

4) What are the disadvantages of shell scripting?

Following are the main disadvantages of shell scripting:

  • A weak design can destroy the entire process and could prove a costly error.
  • If a typing error occurs during the creation then it can delete the entire data as well as partition data.
  • Its initial process is slow and improves gradually.
  • Portability between different operating system is difficult.

You might like: Top 32 Selenium Interview Questions and Answers with PDF Download in 2019

5) What are the different types of variables used in shell script?

Top 36 Shell Scripting Interview Questions There are two types of variables used in shell script:

  • System defined variables: These variables are defined or created by operating system itself.
  • User defined variables: These variables are defined by system users.

6) What is the significance of $#?

$# shows the count of the arguments passed to the script.

7) What is the difference between $* and [email protected]?

[email protected] treats each quoted arguments as separate arguments but $* will consider the entire set of positional parameters as a single string.

8) Use sed command to replace the content of the file (emulate tac command)

Eg:

if cat fille
ABCD
EFGH

Then O/p should be

EFGH
ABCD

sed '1! G; h;$!d' file1

Here G command appends to the pattern space,

h command copies pattern buffer to hold buffer

and d command deletes the current pattern space.

9) Given a file, replace all occurrence of word “ABC” with “DEF” from 5th line till end in only those lines that contains word “MNO”

sed –n '5,$p' file1|sed '/MNO/s/ABC/DEF/'

10) Given a file, write a command sequence to find the count of each word.

tr –s  "(backslash)040" <file1|tr –s  "(backslash)011"|tr "(backslash)040 (backslash)011" "(backslash)012" |uniq –c
where "(backslash)040" is octal equivalent of "space"

“(backslash)011” is an octal equivalent of “tab character” and

“(backslash)012” is an octal equivalent of the newline character.

11) Is it possible to substitute “ls” command in the place of “echo” command?

Yes.


12) What is the syntax of while loop in shell scripting?

Top 36 Shell Scripting Interview Questions The while loop is used to repeat its block of commands a number of times. It is different from for loop, it iterates until the while condition is no longer true.

  1. while [ test_condition ]  
  2. do  
  3. commands…  
  4. done   

13) In my bash shell, I want my prompt to be of format ‘$”Present working directory”: “hostname”> and load a file containing a list of user-defined functions as soon as I log in, how will you automate this?

In the bash shell, we can create a “.profile” file which automatically gets invoked as soon as I log in and write the following syntax into it.

export PS1='$ `pwd`:`hostname`>' .File1

Here File1 is the file containing the user-defined functions and “.” invokes this file in current shell.

14) Explain about “s” permission bit in a file?

“s” bit is called “set user id” (SUID) bit.

“s” bit on a file causes the process to have the privileges of the owner of the file during the instance of the program.

For example, executing “passwd” command to change the current password causes the user to writes its new password to shadow file even though it has “root” as its owner.

15) I want to create a directory such that anyone in the group can create a file and access any person’s file in it but none should be able to delete a file other than the one created by himself.

We can create the directory giving read and execute access to everyone in the group and setting its sticky bit “t” on as follows:

mkdir direc1
 
chmod g+wx direc1
 
chmod +t direc1

16) What are the 3 standard streams in Linux?

0 – Standard Input

1 – Standard Output

2 – Standard Error

17) How can any user find out all information about a specific user like his default shell, real-life name, default directory, when and how long he has been using the system?

finger “loginName” …where loginName is the login name of the

user whose information is expected.

18) What is the difference between $$ and $!?

$$ gives the process id of the currently executing process whereas $! Shows the process id of the process that recently went into the background.

19) What are zombie processes?

Top 36 Shell Scripting Interview Questions These are the processes which have died but whose exit status is still not picked by the parent process. These processes even if not functional still have its process id entry in the process table.

20) How will you copy a file from one machine to other?

We can use utilities like “ftp,” “scp” or “rsync” to copy a file from one machine to other.

E.g., Using ftp:

FTP hostname

>put file1

>bye

Above copies, file file1 from the local system to destination system whose hostname is specified.

21) How variables can be wiped out?

Variables can be wiped out or erased using the unset command.

Example:

$ a =20

$ unset a

Upon using the above command the variable ‘a’ and its value 20 get erased from shell’s memory.

CAUTION: Be careful while using this unset command.

22) What are positional parameters? Explain with an example.

Positional parameters are the variables defined by a shell. And they are used whenever we need to convey information to the program. And this can be done by specifying arguments at the command line.

There are totally 9 positional parameters present i.e. from $1 to $9.

Example: $ Test Indian IT Industry has grown very much faster

In the above statement, positional parameters are assigned like this.

$0 -> Test (Name of a shell program/script)

$1 ->Indian

$2 -> IT and so on.

23) What does the. (dot) indicate at the beginning of a file name and how should it be listed?

A file name which begins with a. (dot) is called as a hidden file. Whenever we try to list the files it will list all the files except hidden file.

But it will be present in the directory. And to list the hidden file we need to use –a option of ls. i.e. $ ls –a.

24) Generally, each block in UNIX is how many bytes?

Ans: Generally, each block in UNIX is of 1024 bytes.

25) By default, a new file and a new directory which is being created will have how many links?

The new file contains one link. And a new directory contains two links.

26) What are the 3 standard streams in Linux?

0 – Standard Input1 – Standard Output2 – Standard Error

27) I want to read all input to the command from file1 direct all output to file2 and error to file 3, how can I achieve this?

command <file1 1>file2 2>file3

28) What will happen to my current process when I execute a command using exec?

“exec” overlays the newly forked process on the current process; so when I execute the command using exec, the command gets executed on the current shell without creating any new processes.

E.g., Executing “exec ls” on command prompt will execute ls and once ls exits, the process will shut down

29) How will you emulate wc –l using awk?

awk ‘END {print NR} fileName’

30) Given a file find the count of lines containing the word “ABC”.

grep –c “ABC” file1

31) What are the three modes of operation of vi editor? Explain in brief.

Top 36 Shell Scripting Interview Questions The three modes of operation of vi editors are,

(i) Command Mode: In this mode, all the keys pressed by a user are interpreted as editor commands.

(ii) Insert Mode: This mode allows for insertion of a new text and editing of an existing text etc.

(iii) The ex-command Mode: This mode allows a user to enter the commands at a command line.

32) What is the alternative command available to echo and what does it do?

tput is an alternative command to echo.

Using this, we can control the way in which the output is displayed on the screen.

33) How to find out the number of arguments passed to the script?

The number of arguments passed to the script can be found as shown below.

echo $ #

34) What are control instructions and how many types of control instructions are available in a shell? Explain in brief.

Control Instructions are the ones, which enable us to specify the order in which the various instructions in a program/script are to be executed by the computer. Basically, they determine a flow of control in a program.

There are 4 types of control instructions that are available in a shell.

  • Sequence Control Instruction – This ensures that the instructions are executed in the same order in which they appear in the program.
  • Selection or Decision Control Instruction – It allows the computer to take a decision as to which instruction is to be executed next.
  • Repetition or Loop Control Instruction – It helps a computer to execute a group of statements repeatedly.
  • Case-Control Instruction – This is used when we need to select from several alternatives.

35) What are Loops and explain three different methods of loops in brief?

Top 36 Shell Scripting Interview Questions Loops are the ones, which involve repeating some portion of the program/script either a specified number of times or until a particular condition is being satisfied.

3 methods of loops are:

  • For loop – This is the most commonly used loop. For loop allows specifying a list of values which the control variable in the loop can take. The loop is then executed for each value mentioned in the list.
  • While loop – This is used in a program when we want to do something for a fixed number of times. While the loop gets executed until it returns a zero value.
  • Until loop – This is similar to while loop except that the loop executes until the condition is true. Until loop gets executed at least once till it returns a non-zero value.

36) What is IFS?

IFS stands for Internal Field Separator. And it is one of the system variables. By default, its value is space, tab, and a new line.

It signifies that in a line where one field or word ends and another begins.