Interview-Shell Scripting
Interview-Shell Scripting
Interview-Shell Scripting
Questions
© Copyright by Interviewbit
Contents
Text files containing commands to execute by a shell are called shell scripts. In this,
long and repetitive series of commands are compiled into a single script that can be
stored and executed at any time, thereby reducing programming efforts. In shell
scripts, repetitive work is largely avoided. The most common reason for using shell
scripting is to program operating systems of Windows, UNIX, Apple, etc. In addition,
companies use this script to develop an operating system with their own features. In
terms of system-level operations, it is considered to be the easiest programming
language to use. A shell script is referred to as a batch file in DOS operating system,
and EXEC in IBM's mainframe VM operating systems. Shell scripts can be used for the
following applications:
Run it as follows:
$ bash hello.sh
# prints
hello world
The C shell allows you to alias commands easily, whereas Bourne Shell does not
allow this.
In the C shell, long commands can be used repeatedly, but not in Bourne.
Bourne does not have access to the command history, but the C shell does.
In the case of C, there is no need to repeatedly type the command.
variable ="Hello"
echo $variable
Shell scripts or programs o en contain shebang at the top. In general, the Shebang
(the series of characters "#!") is used to indicate the operating system to use a
particular interpreter to process the rest of the file. Here, the symbol '#' is referred to
as hash, and "!" is referred to as bang. This usually aids developers in avoiding
repetitive work. Scripts are generally executed by the engine; therefore, shebang
determines the location of the engine that will be used to run the script.
Example:
#!/bin/bash
In addition, this line also specifies which shell to use.
23. Name the command that can be used to find out the number
of arguments passed to the script?
The following command will display the number of arguments passed to the script:
echo $ #
Example:
script.sh &
command &
The backup is taken using the tar command. Tar is an acronym for tape archive and is
used to create backups using tar, gzip, and bzip. Files can be saved and restored from
and to a tape using this command. In this, files and directories are generally
compressed into tarballs, an archive file. For this purpose, it is among the most
widely used commands. Furthermore, the tarball can be easily moved from one
server to another.
As the two strings are identical, this command prints "0" because the expression is
true.
33. Write down the Syntax for all the loops in Shell Scripting.
In shell scripting, you can use three looping statements as given below:
while statement
for statement
until statement
Syntax is as follows:
For Loop: Loops that operate on lists of items are known as loops. Each item in
the list receives the same set of commands.
Syntax:
Example:
for a in 1 2 3 4 5 6 7 8 9 10
do
if [ $a == 3 ]
then
break
fi
echo "Iteration no $a"
done
Output”
$bash -f main.sh
Iteration no 1
Iteration no 2
While Loop: It involves evaluating a command first and then executing a loop
based on the result. The loop will be terminated if the command raises to false.
Syntax:
while command
do
Statement to be executed
done
Example:
a=0
while [ $a -lt 3 ]
do
echo $a
a=`expr $a + 1`
done
Output:
$bash -f main.sh
0
1
2
3
until command
do
Statement to be executed until command is true
done
Example:
a=0
until [ $a -gt 3 ]
do
echo $a
a=`expr $a + 1`
done
Output:
$bash -f main.sh
0
1
2
3
Example:
Executing the "passwd" command to change the password, for example, allows the
user to write the new password to the shadow file even though its owner is "root".
In this case, “s” command in sed replaces the string Hi with “Hello”.
Output:
Hello Gourav
Hello Aisha
Hello Rohan
Hello Gourav
The above script prints only the first word i.e., Hello from each line.
Output:
Hello
Hello
Hello
/bin/bash
/sbin/nologin
/bin/ksh
/bin/csh
46. Name the command that one should use to know how long
the system has been running.
Using the Uptime command, you can see how long your system has been active. You
can also determine the number of users with running sessions, and the average
system load for 1, 5, and 15 minutes. The information displayed at once can also be
filtered based on your specified options.
Example: $ uptime
Entering the above command at the shell prompt, namely $ uptime, will produce the
following output:
9:21am up 86 day(s), 11:46, 3 users, load average: 2.24, 2.18, 2.16
grep “apple” file1.txt //Displays all the lines with the word “appl
grep “apple” file1.txt file2.txt //Scan multiple documents and search the word “appl
Find command: The FIND command searches for files and folders based on their
size, modification time, and access time. You can use this command to search
files and directories. Find command have the following syntax:
find <path> <search criteria> <action>
Example:
find . –name file1.txt //Command will find file1.txt in the current directory.
function_name ()
{
list of commands
}
Function_name is the name of your function, and that's what you'll use to call it from
anywhere in your script. The function name must be followed by parentheses, then a
list of commands enclosed in braces.
Example:
The following example shows how function can be used:
#!/bin/sh
# Define your function here
Hello ()
{
echo "Hello World"
}
$./test.sh
Hello World
Conclusion
You can automate a lot of repetitive manual tasks with shell scripting, which is an
easy and powerful programming method. Modern programming languages offer
many of the features that make them appealing to programmers. DevOps engineers
and automation testers o en find this concept vital in preparing for interviews. Shell
scripting allows the development of simple to complex scripts. A number of daily
tasks can be automated using shell scripting as well. We thought of making your job
easier by putting together an ensemble of the most frequently asked interview
questions and sample answers on shell scripting. These interview questions will
definitely help you understand that shell is basically an interface between the user
and the operating system, which interprets user commands to be executed by the
kernel or operating system.
Useful Resources:
Unix Interview
Automation Testing Interview
Devops Interview
Css Interview Questions Laravel Interview Questions Asp Net Interview Questions