0% found this document useful (0 votes)
447 views

20 UNIX Shell Scripting Interview Questions..

20 UNIX Shell Scripting Interview Questions..
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
447 views

20 UNIX Shell Scripting Interview Questions..

20 UNIX Shell Scripting Interview Questions..
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

LIVEFIRE LABS

Online UNIX Training with Hands-on Internet Lab Contact Phone: 1.888.843.1637

Home Testimonials Demo FAQ Login About Contact

UNIX Tutorials, Tips, Tricks and Shell Scripts You are here: Home UNIX Tutorials, Tips, Tricks and Shell Scripts

20 UNIX Shell Scripting Interview Questions...and Answers!!! - Part I


Absolutely the best IT class I
have ever taken online. As someone who has written a large number of both simple and complex UNIX shell scripts during the
past 15 years, the following are some basic shell scripting interview questions that I would ask if I needed
to hire someone to write new shell scripts or maintain preexisting scripts for me.
Kenneth R, Corona del Mar,
California
These type of questions would help me to determine If the candidate for the position has spent any time
actually writing shell scripts, either on the job or as part of a shell scripting course or training class, or has
just read about them in books or online.
The LiveFire Labs support
was excellent and very
Let's get started...
helpful, a really genuine
company o ering a truly
(1) What is a UNIX shell?
great learning experience.
The UNIX shell is a program that serves as the interface between the user and the UNIX operating system.
It is not part of the kernel, but communicates directly with the kernel. The shell translates the commands
Andrew Porter, Angelite LTD, you type in to a format which the computer can understand. It is essentially a command line interpreter.
Chippenham Wiltshire, UK

Details:
Visual representation of the UNIX operating system environment:

List of commonly used UNIX shells:

The Bourne Shell (sh)


The C Shell (csh or tsch)
The Bourne Again Shell (bash)
The Korn Shell (ksh)

(2) What needs to be done before you can run a shell script from the command line prompt?
You need to make the shell script executable using the UNIX chmod command.

Details:
This chmod command makes the shell script le "example1" executable for the user (owner) only:
$ chmod u+x example1
Do you know
Python?
this syntax makes it executable for all (everyone):
Develop your own web
crawler/scraper and simple $ chmod a+x example1
search engine while learning how
to program with our NEW online
Python Programming course!
You can optionally use octal notation to set UNIX permissions using the chmod command (e.g., $ chmod
755 example1). This topic is beyond the scope of this article, but you can nd more information by
LEARN MORE >>
entering "unix le permissions chmod numeric notation" in your favorite search engine.

Name:
(3) How do you terminate a shell script if statement?
Email: With , which is "if" spelled backwards.

Subscribe Details:
The shell script example below uses an if statement to check if a le assigned to the variable my le exists
Subscribe to be and is a regular le:
notified of new
courses and #!/bin/ksh
qualify for the my le=$1
chance to win a if [ -f $my le ]
FREE Python
then
Programming
echo "$my le exists"
course enrollment.
(ONLY confirmed
exit 0
subscriptions are
eligible to win.)

(See shell scripting interview question #6 below if you do not know what $1 in this example means.)
READ MORE ABOUT
PYTHON >>
(4) What UNIX operating system command would you use to display the shell's environment
variables?
Running the "env" command will display the shell environment variables.

Details:
Sample env command output:

$ env
HISTFILE=/home/l /.history
PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin
SHELL=/bin/ksh
HOSTNAME=live relabs.com
USER=l
MAIL=/var/spool/mail/l
HOME=/home/l
HISTSIZE=1000

It would also be good to understand the purpose of the common shell environment variables that are
listed in the env command output.

(5) What code would you use in a shell script to determine if a directory exists?
The UNIX test command with the -d option can be used to determine if a directory exists.

Details:
The following test command expression would be used to verify the existence of a speci ed directory,
which is stored in the variable $mydir:

if [ -d $mydir ]
then
command(s)
If the value stored in the variable mydir exists and is a directory le, the command(s) located between
then and will be executed.

You can consult the test command's man page ("$ man test") to see what test command options are
available for use.

(6) How do you access command line arguments from within a shell script?
Arguments passed from the command line to a shell script can be accessed within the shell script by
using a $ (dollar sign) immediately followed with the argument's numeric position on the command line.

Details:
For example, $1 would be used within a script to access the rst argument passed from the command
line, $2 the second, $3 the third and so on. Bonus: $0 contains the name of the script itself.

(7) How would you use AWK to extract the sixth eld from a line of text containing colon (:) delimited
elds that is stored in a variable called passwd_line?
echo $passwd_line | awk -F: '{ print $6 }'

Details:
Consider this line of text stored in the variable $passwd_line -

$ echo $passwd_line
mail:x:8:12:mail:/var/spool/mail:
$

(Background: The lines in the system passwd le are delimited (separated) by a colon (:)...$passwd_line
contains a single line from the passwd le.)

The output of the echo command is piped to AWK. The -f option for the awk command informs awk of
what the eld separator is (colon in this example), and print $6 instructs awk to print the 6th eld in the
line.

(8) What does 2>&1 mean and when is it typically used?


The 2>&1 is typically used when running a command with its standard output redirected to a le. For
example, consider:

command > le 2>&1

Anything that is sent to command's standard output will be redirected to " le" in this example.

The 2 (from 2>&1) is the UNIX le descriptor used by standard error (stderr). Therefore, 2>&1 causes the
shell to send anything headed to standard error to the same place messages to standard output (1) are
sent...which is " le" in the above example.

To make this a little clearer, the > in between "command" and " le" in the example is equivalent to 1>.

Before you continue reading... Has this article been helpful to you? Would it bene t others? If you
answered "yes" to either question, kindly share the page.

MORE READERS = MORE FUTURE ARTICLES


Thank you for sharing.

(9) What are some ways to debug a shell script problem?


Although this is somewhat dependent on what the problem is, there are a few commonly used methods
for debugging shell script problems.

One method, which is frequently used across all programming languages, is to insert some debug
statements within the shell script to output information to help pinpoint where and why the problem is
being introduced.

Another method speci c to shell scripting is using "set -x" to enable debugging.

Details:
Consider the following shell script example (notice that "set -x" is commented out at this time):

#!/bin/ksh
#set -x
i=1
while [ $i -lt 6 ]
do
print "in loop iteration: $i"
((i+=1))
done
exit

This script will produce the following output:

$ ./script1
in loop iteration: 1
in loop iteration: 2
in loop iteration: 3
in loop iteration: 4
in loop iteration: 5
$

If we uncomment (remove the #) from the "set -x" line in the script, this is what is displayed when the
script runs:

$ ./script1
+ i=1
+ [ 1 -lt 6 ]
+ print in loop iteration: 1
in loop iteration: 1
+ let i+=1
+ [ 2 -lt 6 ]
+ print in loop iteration: 2
in loop iteration: 2
+ let i+=1
+ [ 3 -lt 6 ]
+ print in loop iteration: 3
in loop iteration: 3
+ let i+=1
+ [ 4 -lt 6 ]
+ print in loop iteration: 4
in loop iteration: 4
+ let i+=1
+ [ 5 -lt 6 ]
+ print in loop iteration: 5
in loop iteration: 5
+ let i+=1
+ [ 6 -lt 6 ]
+ exit
$

In addition to displaying the intended output ("in loop iteration" lines), enabling debugging with "set -x"
also shows each line of execution preceded by a plus sign (+).

Although this can become unwieldly for larger shell scripts, it should be obvious how useful this can be
when debugging a shell script to identify the root cause of a problem.
(10) Within a UNIX shell scripting loop construct, what is the di erence between the break and
continue?
Using break within a shell scripting loop construct will cause the entire loop to terminate. A continue will
cause the current iteration to terminate, but the loop will continue on the next iteration.

Do you need to practice writing and executing UNIX shell scripts (on a REAL SERVER) before the
interview? With a highly concentrated e ort, either of these online courses can be completed within 24
hours...

UNIX and Linux Operating System Fundamentals contains a very good "Introduction to UNIX Shell
Scripting" module, and should be taken if you are new to the UNIX and Linux operating system
environments or need a refresher on key concepts.

UNIX Shell Scripting is a good option if you are already comfortable with UNIX or Linux and just
need to sharpen your knowledge about shell scripting and the UNIX shell in general.

...or you can optionally select a subset of modules from the course to focus on prior to the interview and
then return to work on the remaining topics as your schedule permits.

Both courses include access to an Internet Lab system for completing the course's hands-on exercises,
which are used to re-enforce the key concepts presented in the course. Any questions you may have
while taking the course are answered by an experienced UNIX technologist.

Thanks for reading, and best of luck with your shell scripting interview questions and your interview in
general!!!

Popular Online Courses

Select a course for enrollment


details.

UNIX and Linux Fundamentals Python Fundamentals and Beyond Korn/BASH Shell Scripting
Operating System Basics Learn Python Programming UNIX/Linux Shell Scripting

Contact Details

Address: 10004 Wurzbach Rd #304


LiveFire Labs provides quality, San Antonio, TX 78230
a ordable, and globally accessible
online training to students who desire Phone: 1.888.843.1637

to learn UNIX and related technologies. Email: info@live relabs.com

Copyright 2002-2014 by LiveFire Labs. All Rights Reserved. FAQ Tip, Trick or Shell Script Contact

You might also like