Practical Interview Questions and Answers on Linux Shell Scripting

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Use our Linode referral link if you plan to buy VPS (it starts at only $10/month).
  4. Support us via PayPal donate - Make a Donation
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

6 Responses

  1. Joseph in Atlanta says:

    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.

    • Aaron Kili K says:

      @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.

  2. Joseph in Atlanta says:

    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.

    • Aaron Kili K says:

      @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.

  3. Thanks! That was useful escpecially reversing a given number.

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

Join Over 300K+ Linux Users
  1. 177,942
  2. 8,310
  3. 37,548

Are you subscribed?