Practical Interview Questions and Answers on Linux Shell Scripting

With the overwhelming response we are getting on the Interview Series articles, the first of its kind on any Linux How-to website in the form of Likes, Feedbacks in comment as well as on personal Email Address make us going from one article to the next article.

Here is the link to the Interview Series articles already published on Tecmint.com, where we have covered lots of topics viz., FTP, MySQL, Apache, Scripting, Linux Commands, etc.

Practical Interview Question on Shell  Scripting
Practical Interview Question on Shell Scripting

Continuing to the above series here we are coming with another 5 wonderful Linux Interview Questions and their answer. Your (The Tecmint.com Readers and frequent Visitors) support is always needed to make it successful.

1. Write a shell script to get current date, time, user name and current working directory.
Answer : The commands logname, date, who I am and pwd will output username, current date and time, and current working directory. Just implementing these commands in the script and making it a bit interactive.

Now create a file called ‘userstats.sh‘ and add the following code to it.

#!/bin/bash 
echo "Hello, $LOGNAME" 
echo "Current date is `date`" 
echo "User is `who i am`" 
echo "Current directory `pwd`"

Place execute permission and run the script as shown below.

# chmod 755 userstats.sh 
# ./userstats.sh
Sample Output
Hello, avi 
Current date is Sat Jun  7 13:05:29 IST 2014 
User is avi      pts/0        2014-06-07 11:59 (:0) 
Current directory /home/avi/Desktop
2. Write a Shell Script that adds two numbers if provided as the command Line Argument and if the two numbers are not entered it outputs an Error Message along with a one-Line of how-to use description.
Answer : Here is the simple Shell Script along with description which adds two number if provided as Command-Line argument, if not it throws error with single line on how to use the script.

Again create a file called ‘two-numbers.sh‘ and add the following content to it.

#!/bin/bash 
# The Shebang

if [ $# -ne 2 ] 
# If two Inputs are not received from Standard Input

then 
# then execute the below statements

    echo "Usage - $0   x    y" 
    # print on standard output, how-to use the script (Usage - ./1.sh   x    y )

    echo "        Where x and y are two nos for which I will print sum" 
    # print on standard output, “Where x and y are two nos for which I will print sum ”

    exit 1 
    # Leave shell in Error Stage and before the task was successfully carried out.

fi 
# End of the if Statement.

    echo "Sum of $1 and $2 is `expr $1 + $2`"
    # If the above condition was false and user Entered two numbers as a command Line Argument,   
       it will show the sum of the entered numbers.

Set executer permission on the file and run the script as shown below.

# chmod 755 two-numbers.sh

Condition 1: Running the script without entering two numbers as command line argument, you will get the following output.

Sample Output
# ./two-numbers.sh

Usage - ./two-numbers.sh   x    y 
        Where x and y are two nos for which I will print sum

Condition 2: When Numbers are entered as command line argument you will get the result as shown.

$ ./two-numbers.sh 4 5 

Sum of 4 and 5 is 9

Hence the above shell script fulfills the condition as suggested in the question.

3. You needs to print a given Number say 10572, in reverse order using a Shell script such that the input is provided using command Line Argument only. If the input data is not provided as Command Line Argument, it should throw and error and should suggest, how to use the script. Write the script but before that tell me the algorithm that needs to be implemented here.

Algorithm

  1. 1. Let the Input Number = n
  2. 2. Set rev=0, sd=0 (Reverse and single digitis set to 0)
  3. 3. n % 10, will find and give single left most digit
  4. 4. reverse number is generated as rev * 10 + sd
  5. 5. Decrease Input Number (n) by 1.
  6. 6. if n > 0, then goto step 3 else goto setp 7
  7. 7. Print rev

Now again, create a file called ‘numbers.sh‘ and add the following given code it.

#!/bin/bash 
if [ $# -ne 1 ] 
then 
    echo "Usage: $0   number" 
    echo "       I will find reverse of given number" 
    echo "       For eg. $0 0123, I will print 3210" 
    exit 1 
fi 

n=$1 
rev=0 
sd=0 

while [ $n -gt 0 ] 
do 
    sd=`expr $n % 10` 
    rev=`expr $rev \* 10  + $sd` 
    n=`expr $n / 10` 
done 
    echo  "Reverse number is $rev"

Grant a execute permission on the file and run the script as shown below.

# chmod 755 numbers.h

Condition 1: When Input is not provided as command line argument, you will get the following output.

Sample Output
./numbers.sh

Usage: ./numbers.sh  number 
       I will find reverse of given number 
       For eg. ./2.sh 123, I will print 321

Condition 2: When Input was provided as command line Argument.

$ ./numbers.sh 10572 

Reverse number is 27501

The above script worked perfectly and the output was just what we needed.

4. You are supposed to calculate a real number calculation directly from terminal and not any shell script. What will you do (say the real numbers are 7.56 and 2.453)?
Answer : We need to use bc command in a special way as described below. The output of echo 7.56 + 2.453 should be pipelined to bc.

For example, run the following command to calculate numbers in real time using bc command as shown.

$ echo 7.56 + 2.453 | bc

10.013
5. You are supposed to find the value of pi upto 100 places of decimal, what is the easiest way to get the result done.
Answer : The easiest way to find the value of pi, correct upto 100 places of decimal we just need to issue the below command.
# pi 100 

3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067

Obviously! We must have package ‘pi‘ installed. Just do a apt or yum to get a required package to install ‘pi‘ onto the distribution you are using.

That’s all for now. I’ll be here again with another interesting article very soon. Till then stay tuned and connected to Tecmint.com. Don’t forget to provide us with you valuable feedback in the comment section below.

If you read this far, tweet to the author to show them you care. Tweet a thanks
Ronav Saive
A Passionate GNU/Linux Enthusiast and Software Developer with over a decade in the field of Linux and Open Source technologies.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

10 thoughts on “Practical Interview Questions and Answers on Linux Shell Scripting”

  1. Hello, I’m not sure if this is the right section to post my question.

    I have a remote Ubuntu server with Xfce running on it. Here an application I would like to run via local script (remote). Any help with your great articles is appreciated, thank you in advance.

    Reply
  2. Question #5 (display PI to 100 places) The “pi” command is an obscure command on Unix/Linux, that 75% of root sysadmin professionals don’t have occasion to use with 10yrs of real-job work.

    Shell scripts for business are more about server control, admin, performance tuning, storage management, databases, networking, security, monitoring, clusters, install/upgrade, reporting, and user-id setup… so they seldom need value of “pi”, let alone to 100 digits.

    The “pi” command is not even present for many Unix versions such as HP-UX, IBM’s AIX, Solaris, etc… which means you generally would not use that command in a shell script that can be used with MANY kinds of servers.

    Alternately, the “bc” command line calculator IS present on almost every form of Unix, and can also be used to calculate the value of “pi” like this:

    $  echo "scale=100 a(1)*4" | bc -l              ## Note: function a(x) is "arc-tangent(x)
    

    SO: While this is a valid question/answer for Linux, it may not be an accurate assessment of someone’s real-world experience on the job.

    Reply
    • @Joseph

      Your analysis is on point here, however, this command : $ echo “scale=100 a(1)*4” | bc -l actually has a syntax error. It is missing a minor but important ; sign after the scale value, the correct syntax as per Bash shell is:

      $ echo “scale=100; a(1)*4” | bc -l

      You could be using another shell, you can let us know, above all thanks for your vital concerns and productive feedback.

      Reply
    • Hello There,

      Thank you, Your blog is indeed quite interesting around Practical Interview Questions and Answers on Linux Shell Scripting! I agree with you on lot of points!

      By the way do you have any YouTube videos, would love to watch it. I would like to connect you on LinkedIn, great to have experts like you in my connection (In case, if you don’t have any issues).

      Thank you,
      Kevin

      Reply
  3. Script for question#2 (adding two parameter numbers) uses expr $1 + $2` which is an OLD shell scripting syntax for addition, that has been around since 1973, and is less efficient because it uses a separate binary process just for the math.

    The preferred form in Korn/ksh shell or bash is: $(( $1 + $2 )) which runs faster because it uses the built-in math support from the shell, rather than separate process.

    Shell scripts using `expr` Will run OK, but is not considered ‘good form’ and most shell script writers will not give that answer… so I would not want somebody giving an interview to think that $(( $1 + $2 )) is in-correct.

    Reply
    • @Joseph

      You are absolutely correct, Although they both methods work just fine, however, using $(( $1 + $2 )) is more efficient than `expr $1 + $2`. I often use the former for mathematical expressions in shell scripts.

      Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.