Shell is an interface between the user and the kernel. Even it has only one kernel; a system can have many shells which can run simultaneously. whenever a user enters a command the shell communicates with the kernel to execute it and then displays the output to the user. Shell Scripting is a collection of commands put together into a file. The script is a command or an instruction given to process and these set of instructions put together in a file to perform some task.
In this article, you can go through the set of Shell Script interview questions most frequently asked in the interview panel. This will help you crack the interview as the topmost industry experts curate these at HKR training.
Let us have a quick review of the Shell Script interview questions
Ans: There are two variables used in the Shell Script.
Ans: There are primarily two kinds of shells in Linux OS, namely, Bourne Shell and C-Shell. Examples of derivative from each are as follows;
Ans: Type the below code in q15.sh and run it.
#!/bin/sh
var=$?
if var=0
then
echo "Script was Run successfully"
else
echo "Script was unsuccessful"
fi
Output:
$ ./q15.sh
Script was Run successfully
Ans: C is a more preferable option in the following cases:
Ans: "rm" removes each specified file argument (provided that it is a valid path name). If you specify either. or.. as the final component of the path name for a file, rm displays an error message, and moves onto the next file. If you specify a file you do not have write permission for, rm asks you for confirmation.
Ans: They are process IDs given to processes. A PID can vary from 0 to 65535.
Ans:
#!/bin/sh
a=1
b=1
echo $a
echo $b
for I in 1 2 3 4 5 6 7 8
do
c=a
b=$a
b=$(($a+$c))
echo $b
done
Ans: There are different operations that can be performed in different modes. while working with Vi Editor.
Ans: Shell scripting is used to program command line of an operating system. Shell Scripting is also used to program the shell which is the base for any operating system. Shell scripts often refer to programming UNIX. Shell scripting is mostly used to program operating systems of windows, UNIX, Apple, etc. Also this script is used by companies to develop their own operating system with their own features.
Ans: Shell Scripting is important because of the following reasons.
Ans: There are many advantages of shell scripting some of them are, one can develop their own operating system with relevant features best suited to their organization than to rely on costly operating systems. Software applications can be designed according to their platform.
Ans:
Ans:
Syntax in ksh:
Set –A arrayname= (element1 element2 ….. element)
In bash
A=(element1 element2 element3 …. elementn)
Ans: The blocks in the file system are as follows.
Ans: The three different security provisions are as follows.
Ans:
For Loop:
for var in word1 word2 ... wordN
do
Statement(s) to be executed for every word.
done
While Loop:
while command
do
Statement(s) to be executed if the command is true
done
Until Loop:
until command
do
Statement(s) to be executed until the command is true
done
Ans: You can find all shells available in the system with $ cat /etc/shells.
Example:
$ cat /etc/shells
Execution over Shell Interpreter/Editor:
$ cat /etc/shells
Output:
/bin/sh
/bin/bash
/sbin/nologin
/bin/ksh
/bin/dash
/bin/tcsh
/bin/csh
Ans: Below are common methods applied to debug the problems in the script.
Ans: sed is an acronym for stream editor. 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.
Syntax: sed options file
Example:
Execution over Shell Interpreter/Editor
/u/user1/Shell_Scripts_2020> echo "Hello Word" | sed 's/Hello/Hi/'
Here ‘s’ command present in sed will replace string Hello with Hi.
Output:
Hi World
Ans: Shebang is a # sign followed by an exclamation i.e. !. In general, it is seen at the beginning or top of the script/program. Developers will use this to avoid repetitive work. Shebang determines the location of the engine which is to be used in order to execute the script.
Here ‘#’ symbol is called hash and ‘!’ is called a bang.
Example: #!/bin/bash
The above line also tells which shell to use.
Ans: The utilities simply connect the system with a database server. The users have to perform many tasks which are relevant and important in scripting. The scripting requires more data and information and it is not always possible to keep the same at that particular location only. The users have to make sure of an error-free outcome in this approach.
Ans: The utilities such as "ftp," "scp" or "rsync" to copy a file from one machine to another.
Example:
Using ftp:
FTP hostname
>put file1
>bye
The above command copies file file1 from the local system to the destination system whose hostname is specified.
Ans: In the shell scripting, there is a concept of tailing which can be applied to monitor a log file frequently. It is done by using the tail-f filename. It enables the users to display the previous ten lines on the output. The same reflects the part of the file which is updating continuously.
Ans: There are three different commands available to check the disk usage.
Ans: Type the following code in the “q52.sh” file
#!/bin/sh
echo "Hello, $LOGNAME"
echo "Today's date is `date`"
echo "Username is `who i am`"
echo "Current directory is `pwd`"
Output:
[[email protected] ~]$ ./q52.sh
Hello, hkr
Today's date is Wed Sep 9 02:23:58 EDT 2020
Username is hkr pts/0 2020-09-09 02:20 (:0)
Current directory is /home/hkr
Ans: Type and run the following code in the q34.sh file.
#!/bin/sh
echo ${variable:x:y}
#x - start position
#y - length
variable="My name is Krishna, and I work at Hkr."
echo ${variable:11:7} # will display Krishna
Output:
[[email protected] ~]$ ./q34.sh
Krishna
Ans: Type and run the following code to print PID in q37.sh file.
#!/bin/sh
for PID in $$
do
echo $PID
done
Output:
[[email protected] ~]$ ./q37.sh
7365
Ans: Type and run the following code to print PID in q38.sh file.
!/bin/sh
array=("This" "is" "Shell" "Scripting")
echo ${array[@]} # prints the array elements
echo ${!array[@]} # prints the index of array elements
echo ${array[0]} # prints first array element
Output:
[[email protected] ~]$ ./q38.sh
This is Shell Scripting
0 1 2 3
This
Ans: The backup is made using tar command. It stands for tape archive. The main purpose of this command is used for saving and restoring files to and from an archive medium like tape.
Ans: Positional parameters are the variables defined by a shell. These parameters are used whenever there is a need to convey information to the program. It can be done by specifying arguments at the command line.
There is a total of 9 positional parameters present i.e. from $1 to $9.
Example: $ Test Shell is a Command Line Interpreter.
In the above statement, positional parameters are assigned like this.
$0 -> Test (Name of a shell program/script)
$1 -> Shell
$2 -> is and so on
Ans: There are mainly four important types of shells that are widely used.
Ans: A script may specify “#!/bin/bash” on the first line to denote that the script should always run with bash, rather than another shell. The “/bin/sh” is an executable representing the system shell. It is implemented as a symbolic link pointing to the executable for whichever shell is the system shell.
Ans: The two files of crontab command are.
Ans: You can use ssh to do this:
Syntax:
ssh [email protected] -p sshport
Example:
ssh [email protected] -p 22
After the above command is executed, you will be asked to enter the password.
Ans: You can use the command "comm" as follows:
comm -12 file1 file2 ... 12
This will suppress the content which is unique to the first and second file respectively.
Batch starts on 5th Jun 2023, Weekday batch
Batch starts on 9th Jun 2023, Fast Track batch
Batch starts on 13th Jun 2023, Weekday batch