60 Top Unix Shell Scripting Interview Questions and Answers: Content Delivery Network
60 Top Unix Shell Scripting Interview Questions and Answers: Content Delivery Network
stackpath.com OPEN
Right from a simple to the complex script can be developed using Shell
Scripting. Shell scripting is nothing but series of UNIX commands written in a
plain text file to accomplish a specific task. And with the help of shell
scripting, some tasks of the day to day life can be automated.
There are hardly few documents available over the internet on shell scripting
interview questions and answers. Hence, I have chosen Shell Scripting as my
topic to help those who are in need of it.
Here is the list of “60 Most Important Shell Scripting Interview Questions and
Answers” which covers almost all the aspects pertaining to shell scripting for
the benefit of its users.
Ans: Shell is a command interpreter, which interprets the command which the
user gives to the kernel. It can also be defined as an interface between a user
and operating system.
Ans: The points given below explain the importance of writing shell scripts.
Shell script takes input from the user, file and displays it on the screen.
Shell scripting is very useful in creating your own commands.
It is helpful in automating some tasks of the day to day life.
It is useful for automating system administration tasks.
Mainly it saves time.
Q #4) List some of the common and most widely used UNIX commands.
Ans: There are mainly 4 important types of shells that are widely used.
Q #8) In a typical UNIX environment how many kernels and shells are
available?
Ans: In a typical UNIX environment, only one kernel and many shells are
available.
Ans: A separate compiler is not required to execute a shell program. The shell
itself interprets the command in the shell program and executes them.
Q #10) How many shell scripts come with UNIX operating system?
Ans: There are approximately 280 shell scripts that come with the UNIX
operating system.
When the task is very much complex like writing the entire payroll
processing system.
Where there is a high degree of productivity required.
When it needs or involves different software tools
When it needs or involves different software tools.
Ans: The basis of shell programming relies on the fact that UNIX shell can
accept commands not just only from the keyboard but also from a file.
Ans: 666 i.e. rw-rw-rw- is the default permission of a file when it is created.
Ans: Any task can be accomplished via shell script at the dollar ($) prompt
and vice versa.
Ans: Shell variables are the main part of shell programming or scripting. They
mainly provide the ability to store and manipulate information within a shell
program.
Q #17) What are the two types of Shell Variables? Explain in brief.
#1) Unix Defined Variables or System Variables – These are standard or shell
defined variables. Generally, they are defined in CAPITAL letters.
Example: SHELL – This is a Unix Defined or System Variable, which defines
the name of the default working shell.
#2) User Defined Variables – These are defined by users. Generally, they are
defined in lower letters
Example: $ a=10 –Here the user has defined a variable called ‘a’ and assigned
value to it as 10.
Q #18) How are shell variables stored? Explain with a simple example.
Example: $ a=10
In the above statement a=10, the 10 stored in ‘a’ is not treated as a number,
but as a string of characters 1 and 0.
Ans: The lifespan of a variable inside shell script is only until the end of
execution.
Example:
$ a=10
$ readonly a
Ans: 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.
Ans: 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.
$1 ->Indian
$2 -> IT and so on.
Q #23) What does the. (dot) indicate at the beginning of a file name and how
should it be listed?
Ans: 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.
Q #25) By default, a new file and a new directory which is being created will
have how many links?
Ans: New file contains one link. And a new directory contains two links.
PermissionsWeight
r – read 4
w – write 2
x - execute 1
The above permissions are mainly assigned to owner, group and to others i.e.
outside the group. Out of 9 characters first set of 3 characters
decides/indicates the permissions which are held by the owner of a file. The
next set of 3 characters indicates the permissions for the other users in the
group to which the file owner belongs to.
And the last 3 set of characters indicate the permissions for the users who
are outside the group. Out of the 3 characters belonging to each set, the first
character indicates the “read” permission, the second character indicates
“write” permission and the last character indicates “execute” permission.
Ans: The file system is a collection of files which contain related information
of the files.
Q #28) What are the different blocks of a file system? Explain in brief.
Ans: Given below are the main 4 different blocks available on a file system.
File System
Block No. Name of the Block
1st Block Boot Block
2nd Block Super Block
3rd Block Inode Table
4th Block Data Block
Super Block: This block mainly tells about a state of the file system like how
big it is, maximum how many files can be accommodated etc.
Q #29) What are the three different security provisions provided by UNIX for a
file or data?
Ans: Three different security provisions provided by UNIX for a file or data are:
Q #30) What are the three editors available in almost all the versions of UNIX?
Q #31) What are the three modes of operation of vi editor? Explain in brief.
(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
(iii) The ex-command Mode: This mode allows a user to enter the commands
at a command line.
Q #32) What is the alternative command available to echo and what does it
do?
Using this, we can control the way in which the output is displayed on the
screen.
Q #33) How to find out the number of arguments passed to the script?
Ans: The number of arguments passed to the script can be found as shown
below.
echo $ #
Q #34) What are control instructions and how many types of control
instructions are available in a shell? Explain in brief.
Ans: 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.
Q #35) What are Loops and explain three different methods of loops in brief?
Ans: 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.
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 loop gets executed till 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.
Ans: 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.
Ans: The break is a keyword and is used whenever we want to jump out of a
loop instantly without waiting to get back to the control command.
When the keyword break is encountered inside any loop in the program,
control will get passed automatically to the first statement after a loop. A
break is generally associated with an if.
Ans: Continue is a keyword and is used whenever we want to take the control
to the beginning of the loop, by passing the statements inside the loop which
have not yet been executed.
When the keyword continue is encountered inside any loop in the program,
control automatically passes to the beginning of a loop. Continue is generally
associated with an if.
Example:
Output:
$ cat script1 > script2 – Here output of cat command or script1 will go to a
script2.
Execution over Shell Interpreter/Editor
Output:
Output:
Ans: In a shell, we can easily execute multiple scripts i.e. one script can be
called from the other. What we have to do is, we need to mention the name
of a script to be called when we want to invoke it.
Example: In the below program/script upon executing the first two echo
statements of script1, shell script executes script2. Once after executing
script2, the control comes back to script1 which executes a pwd command
and then terminates.
Q #41) Which command needs to be used to know how long the system has
been running?
Ans: uptime command needs to be used to know how long the system has
been running.
Example: $ uptime
Upon entering the above command at shell prompt i.e. $ uptime, the output
should look something like this.
9:21am up 86 day(s), 11:46, 3 users, load average: 2.24, 2.18, 2.16
Output:
Q #42) How to find the current shell which you are using?
Ans: We can find the current shell what we are using with echo $SHELL.
Output:
Ans: We can find all the available shells in our system with $ cat /etc/shells.
Script/Code
Output:
Q #45) How many fields are present in a crontab file and what does each field
specify?
Ans: The crontab file has six fields. The first five fields tell cron when to
execute the command: minute(0-59), hour(0-23), day(1-31), month(1-12), and
day of the week(0-6, Sunday = 0).
Ans: tar is the command which needs to be used to take the backup. It
stands for tape archive. The tar command is mainly used to save and restore
files to and from an archive medium like tape.
Q #48) What are the different commands available to check the disk usage?
Ans: There are three different commands available to check the disk usage.
dfspace – This command is used to check the free disk space in terms of MB.
Ans: The total disk space used by John can be found out as shown below.
du –s/home/John
Example: #!/bin/bash
$ {#variable}
vi –R <File Name>
Q #57) How can the contents of a file inside jar be read without extracting in
a shell script?
Ans: The contents of the file inside a jar can be read without extracting in a
shell script as shown below.
Ans: diff – Basically, it tells about the changes which need to be made to
make files identical
make files identical.
cmp – Basically it compares two files byte by byte and displays the very first
mismatch.
Ans: sed stands for stream editor. And it is used for editing a file without
using an editor. It is used to edit a given stream i.e. a file or input from a
pipeline.
Example:
Here ‘s’ command present in sed will replace string Hello with Hi.
Output:
Ans: awk is a data manipulation utility or command. Hence, it is used for data
manipulation.
Example:
Script/Code
Output:
The above script prints only first word i.e. Hello from each line.
Output:
Conclusion
After going through all the above shell scripting interview questions and
answers, mainly we understood that a shell is an interface between a user
and an operating system which interprets the command entered by a user to
the kernel or operating system.
Hope, this article would have helped you to understand the UNIX and shell
scripting concepts in a simple and better way.
PREV Tutorial