Common Interview Questions on Shell Scripting you cannot ignore in your upcoming interview

Common Interview Questions on Shell Scripting you cannot ignore in your upcoming interview

In this particular article, we will look into the most common questions that are asked on shell scripting in an interview. The article won't be a long one. Also, I will be sharing some of the commands and scripts as screenshots as I will be running them on my terminal.

So, sit back, relax and see if these commands help you with your interview when it comes to shell scripting.

Q.1) List some of the commonly used shell commands.

Ans) The most commonly used shell commands are as follows:

  • listing: ls

  • debugging: top, sar

  • disk space: df -h

  • copy: cp

  • move or rename: mv

  • filtering: grep

  • creating a directory: mkdir

  • creating an empty file: touch

  • View and edit a file: vim <filename>

  • viewing the contents of the file: cat <filename>

  • print: echo

Q2) Write a sample shell script to list all the processes.

Ans) We start by creating a file with .sh extension. We usually do it using the vim command. We begin by entering the following command: #! /bin/bash. Here, we'll make use of the command ps -ef followed by using the awk command to further filter down the data.

Refer to the below image for further clarity.

To execute this shell script, we need to first make it executable. We do this by granting the required permissions using the command chmod followed by 777 and the shell script file name. What this would do is, it would grant full access to the user, group as well as other users.

Our goal is to make the file executable. We could have just used the command: chmod +x Even this works.

Q3) Write a script to print only errors from the remote log

Ans) curl <URL of the remote log> | grep ERROR

where, curl command is used to retrieve the log file, grep for filtering and pipe command ' | ' to combine curl with grep.

Q4) Write a shell script to print numbers divisible by 3 or 5 and not divisible by 15.


And the output is:

Q5) Write a script to print the number of "s" in the word "Mississippi"

Ans) We approach this by first declaring a variable, for example, 'x', and assigning the word 'Mississippi' to it.


grep -0 "s" <<< "$x" | wc -l

The output would be 4.

Q6) How will you debug the shell script?

using the command set -x. The script will run in debug mode, or in other words, you'll be able to troubleshoot your script.

Ans) Hard link is a backup for the actual file. Suppose, the actual file gets deleted, you'd still have the file data in the hard link.

A soft link is like a shortcut to the actual file. So, if the actual file gets deleted, the soft link also becomes non-functional.

It's a wrap for now. As I had mentioned at the beginning, this would be a short article. I will be coming back with more such interview question examples related to but not restricted to shell scripting. Thanks for your patient reading.