Shell Scripting Interview Questions

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

1. What are the different types of variables used in Shell Script?

Ans: There are two variables used in the Shell Script.

  • System-defined variables are created or defined by the Operating System(Linux) itself. These variables are generally defined in Capital Letters and can be viewed by the “set” command.
  • User-defined variables are created or defined by system users and the values of variables can be viewed by using the command “echo”.

2. What are the different types of commonly used shells on a typical Linux system?

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;

  1. Bourne Shell: Bourne Shell, Bourne-Again Shell, Korn Shell, POSIX Shell.
  2. C-Shell: C-Shell, TENEX C-Shell, Z-Shell.

3. How to check if the previous command was run successfully?

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

4. What makes C shell a more preferable option than the Bourne Shell?

Ans: C is a more preferable option in the following cases:

  • All the commands can be aliased simply with the C shell but it is not possible in case of Bourne Shell.
  • Lengthy commands can be used again and again in C shells. Bourne shell doesn’t allow this in all the cases.
  • The command history can be accessed through the C shell but it cannot be accessed through the Bourne.

5. How Do You Remove A File?

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.

6. What Are Pids?

Ans: They are process IDs given to processes. A PID can vary from 0 to 65535.

7. Write a script to print the first 10 elements of Fibonacci series.

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

8. What are Vi Editor modes available?

Ans: There are different operations that can be performed in different modes. while working with Vi Editor.

  1. Command Mode: Launching Vi automatically starts command mode.
  2. Edit Mode: This mode provides an environment to edit text.
  3. Ex Mode: Vi editor interaction is made available with file processing instructions.

9. What Is Shell Scripting?

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.

10. Why is writing Shell Scripts important according to you?

Ans: Shell Scripting is important because of the following reasons. 

  1. Shell scripting is very useful and powerful while creating customized commands on your own.
  2. There are many tasks which make the process of automation easy.
  3. The users are responsible for providing the input and this clearly means the output if free from errors.
  4. It is time-saving.
  5. Many system administration tasks can be accomplished and can be automated.

11. State The Advantages Of Shell Scripting?

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.

12. What Are The Disadvantages Of Shell Scripting?

Ans:         

  • There are many disadvantages of shell scripting they are
  • Design flaws can destroy the entire process and could prove a costly error.
  • Typing errors during the creation can delete the entire data as well as partition data.
  • Initially process is slow but can be improved.
  • Portbility between different operating system is a prime concern as it is very difficult to   port scripts etc.

13. How to set an array in Linux?

Ans: 

Syntax in ksh:

Set –A arrayname= (element1 element2 ….. element)

In bash

A=(element1 element2 element3 …. elementn)

Shell Scripting Training Certification

  • Master Your Craft
  • Lifetime LMS & Faculty Access
  • 24/7 online expert support
  • Real-world & Project Based Learning

 

14. What are the different blocks of a file system? Explain in brief.

Ans: The blocks in the file system are as follows.

  • Super Block: This block mainly tells about the state of the file system such as how big it is, the maximum number of files that can be accommodated, etc.
  • Boot Block: This block represents the beginning of a file system. It contains the bootstrap loader program, which gets executed while booting the host machine.
  • Inode Table: All the entities in a UNIX are treated as files. So, the information related to these files is stored in an Inode table.
  • Data Block: This block contains the actual file contents.

15. What are the three different security provisions provided by UNIX for a file or data?

Ans: The three different security provisions are as follows.

  1. It provides a unique user id and password to the users for providing authentication, so that unknown or unauthorized cannot access it.
  2. It provides security at the file level by providing read, write and execute permissions to access the files.
  3. It provides security using file encryption. This method allows encoding a file in an unreadable format. Even if someone opens a file they cannot read its contents until and unless it is decrypted.

16. Write down the Syntax for all the loops in Shell Scripting.

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

17. How to find all the available shells in your system?

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

18. How to debug the problems encountered in the shell script/program?

Ans: Below are common methods applied to debug the problems in the script.

  • Debug statements can be inserted in the shell script to output/display the information which helps to identify the problem.
  • Using set -x enables the debugging in the script.

19. Explain in brief about sed command with an example.

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

20. What is Shebang in a shell script?

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.

21. What is the function of utilities that come with the open client driver in the shell scripting?

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.

22. How will you copy a file from one machine to another?

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.

23. In Shell scripting, how can the user frequently monitor a log file that is updating?

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. 

Subscribe to our youtube channel to get new updates..!

 

24. What are the different commands available to check the disk usage?

Ans: There are three different commands available to check the disk usage.

  1. df: It is used for checking the free disk space.
  2. du: It is used for checking the directory wise disk usage.
  3. dfspace: It is used for checking the free disk space in terms of MB.

25. Write a shell script to get current date, time, username and current working directory.

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

26. How to get part of a string variable by using echo command?

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

27. How to print PID of the current shell?

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

Shell Scripting Training Certification

Weekday / Weekend Batches

 

28. How to print all array elements with their respective indexes and print the first array?

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

29. What command needs to be used to take the backup?

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.

30. What are positional parameters? Explain with an example.

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

31. What are the different types of Shells available?

Ans: There are mainly four important types of shells that are widely used.

  1. Bourne Shell (sh).
  2. C Shell (csh).
  3. Korn Shell (ksh).
  4. Bourne Again Shell (bash).

32. What does it mean by #!/bin/sh or #!/bin/bash at the beginning of every script?

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.

33. What are the two files of crontab command?

Ans: The two files of crontab command are.

  1. cron.allow: It decides which users should be permitted from using crontab command.
  2. cron.deny: It decides which users should be prevented from using the crontab command.

34. I want to connect to a remote server and execute some commands, how can I achieve this?

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.

35. I have two files and I want to print the records which are common to both.

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.

Find our upcoming Shell Scripting Training Certification Online Classes

  • 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

 
Global Promotional Image
 

Categories

Request for more information

Saritha Reddy
Saritha Reddy
Research Analyst
A technical lead content writer in HKR Trainings with an expertise in delivering content on the market demanding technologies like Networking, Storage & Virtualization,Cyber Security & SIEM Tools, Server Administration, Operating System & Administration, IAM Tools, Cloud Computing, etc. She does a great job in creating wonderful content for the users and always keeps updated with the latest trends in the market. To know more information connect her on Linkedin, Twitter, and Facebook.