Shell scripting Interview questions with answers


Interview Questions, Linux

Here I have consolidated some of the basic shell scripting interview questions with their possible answers to help you practice and prepare for the interview. Now since these are scripting related questions, so there can be many possible answers, so if you know a better or easier way to work on these questions then use your preferred programming language.

shell-scripting-interview-questions-with-answers

 

Shell scripting interview questions

 

Question:
How can you create a script that will wait for specific output and hence will act according to it? - for instance, wait for “username: ” before sending the username.

Answer:
Linux provides a tool that “expects” a specific string and sending new commands in response which called “expect”.

 

Question:
You want to add logger to your script so how can you send logging messages to the /var/log/messages for your script “MyCoolScript”?

Answer:
Now to write to the messages file, you can use the logger tool which is the syslogd api and send log messages -

logger -t MyCoolScript Starting Application….

 

Question:
Using perl, write a command that will print all the IPs, Bcasts and Masks configured on the server line by line.

Answer:
You need to extract the IPs from the ifconfig first, then run on each line and get the required information -

ifconfig -a | perl -n -l -e '/ addr:([^ ].+)/ and print $1'

 

Question:
Write a shell script that checks if a file (as an argument) has write permissions and accordingly if it is available print “write access approved” else print “no write access”.

Answer:

#!/bin/bash
filename ="$1"
if [ -w "$filename" ]
then
   echo "write access approved"
else
   echo "no write access”;
fi

 

Question:
You have a bash script that does not produce the expected result so how can you debug it?

Answer:
In order to debug a bash shell script, you need to add “-x” to the shell execute line -

“#!/bin/bash -x”

 

Question:
You have a regular user access to a server, with no root permissions, but you need to create a script that requires root permissions to run - how can you manipulate the system to think that you have root permissions, without a real superuser access?

Answer:
Linux provides a tool called fakeroot that allows you to run a “fake root shell” that will present you as root and your id as 0, this will make the system believe that you have root access for the current run.

 

Question:
Write a script that receives one parameters (file name) and checks if the file exists or not - If it does, print “Roger that!” else, print “Huston we’ve got a problem!

Answer:

#!/bin/bash
FILE=$1
if [ -f $FILE ];
then
   echo "Roger that!"
else
   echo "Huston we’ve got a problem"
fi

 

Question:
Write a script that checks if a file, given as an argument, has more than 10 lines or not, if it does - print “Over 10”, else print “Less than 10

Answer:

#!/bin/bash
count=`cat $1 | wc -l`
if [ "$count" -gt "10" ] ;
then
   echo "Over 10"
else
   echo "Less than 10"
fi

 

Question:
You need to create a backup script called “backupMyFiles” which will run every hour, how do you make sure that your script is not already running when you run the script - write a short script that will handle this scenario and will exit with the message “Previous <command> is still running” in case the script is still in the background so that there is no conflict.

Answer:
To check if your script is currently running in the system - you can do it with ps

#!/bin/bash
cmd=namedScript
runningProcs=`ps --no-headers -C${cmd}`
count=`echo $runningProcs|wc -l`

if [ $count -gt 1 ]; then
   echo "Previous $cmd is still running."
   exit 1
fi

 

Question:
Create a Fibonacci function (Fn=Fn-1+Fn-2) using awk (until F20).

Answer:

awk 'BEGIN {
fa=1;
fb=1;
while(++i<=20)
{
  print fa;
  ft=fa;
  fa=fa+fb;
  fb=ft
};
exit}'

 

Question:
Write a script that will go over all the users on the system and will write the last login date of the user, if we don’t have information about the last login then write “No data for <username>

Answer:

#!/bin/sh
for i in `cat /etc/passwd | awk -F: '{print $1; }'` ;
do
  last=`last $i | head -n 1`;
  if [ "$last" != "" ];
  then
     echo `last $i | head -n 1`;
  else
     echo "No data for $i"
  fi
done

 

Question:
How can you check what are the most common commands that you have used in the Linux shell?

Answer:
You can get this information from the history command and sort it by most used.

$ history | awk '{h[$2]++}END{for (c in h){print h[c] " " c}}' | sort -nr | head
3 history
2 ls
1 hostname
1 cd

 

Question:
Write a script that goes to http://www.whatismyip.org/ and then prints “Your IP is: <Result from site>”.

Answer:

#!/bin/sh
ip=`links --source http://www.whatismyip.org/`
echo -n "Your IP is: "
echo $ip

 

Question:
Create a small calculator in bash script which will have an internal function “dosomething” that will receive a math function as an input - mycalc 4+4*4.

Answer:

#!/bin/bash
function dosomething {
echo "${1}"|bc -l;
}

dosomething $1

Question:
Create a script called KillUserProcs that will get a username as an input and will kill all his processes.

Answer:

#!/bin/bash
kill -9 `ps aux|awk -v var=$1 '$1==var { print $2 }'`

 

 

Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment