Discover millions of ebooks, audiobooks, and so much more with a free trial

From $11.99/month after trial. Cancel anytime.

UNIX Shell Scripting Interview Questions, Answers, and Explanations: UNIX Shell Certification Review
UNIX Shell Scripting Interview Questions, Answers, and Explanations: UNIX Shell Certification Review
UNIX Shell Scripting Interview Questions, Answers, and Explanations: UNIX Shell Certification Review
Ebook212 pages1 hour

UNIX Shell Scripting Interview Questions, Answers, and Explanations: UNIX Shell Certification Review

Rating: 4.5 out of 5 stars

4.5/5

()

Read preview

About this ebook

The Ultimate Reference & Learning Guide for the advanced UNIX Operator

Fluency with a UNIX Shell is mandatory for working within the UNIX operating system. Keeping current, however, can be a challenge. With the UNIX Shell Scripting Interview Guide, you will be sure to be current. Using this book to prepare for a job interview or brush up on a scripting programming language will aid any programmer in acquiring new and applicable skills and knowledge. This book contains a complete reference of common scripting errors, performance tuning examples, and common applications. More than just UNIX documentation and open source rhetoric, this guide explains UNIX Shell Scripting from the perspective of the advanced programmer and administrator so you can be sure to come to any interview prepared.

Key topics include:

Graphical user interfaces for UNIX
Advanced features of Bash, Bourne, and Korn
Common errors and troubleshooting
System monitoring and maintenance
Modern command line shells

LanguageEnglish
PublisherEquity Press
Release dateJun 3, 2011
ISBN9781465743978
UNIX Shell Scripting Interview Questions, Answers, and Explanations: UNIX Shell Certification Review

Read more from Equity Press

Related to UNIX Shell Scripting Interview Questions, Answers, and Explanations

Related ebooks

Related articles

Reviews for UNIX Shell Scripting Interview Questions, Answers, and Explanations

Rating: 4.5 out of 5 stars
4.5/5

4 ratings1 review

What did you think?

Tap to rate

Review must be at least 10 words

  • Rating: 5 out of 5 stars
    5/5
    very well explained

Book preview

UNIX Shell Scripting Interview Questions, Answers, and Explanations - Equity Press

BASIC QUESTIONS AND ANSWERS:

Questions most likely to be asked by beginners

Question 01: Shell Script

What is shell script anyway?

A: A shell script, in its most basic form, is simply a collection of operating system commands put into a text file in the order they are needed for execution. Using any of the shells mentioned so far, a text file containing the commands listed in example basic shell script would work every time the script was executed.

Example basic shell script:

#!/bin/sh

rm -f /tmp/listing.tmp> /dev/null 2>&1

touch /tmp/listing.tmp

ls -l [a-z]*.doc | sort> /tmp/listing.tmp

lpr -Ppostscript_1 /tmp/listing.tmp

rm -f /tmp/listing.tmp

Of course, not all scripts are this simple but it does show that ordinary UNIX commands can be used without any extra, fancy scripting constructs. If this script was executed any number of times the result would be the same, a long listing of all the files starting with lower case letters and ending with a doc extension from the current directory printed out on your local PostScript printer. Let’s start to look at this in detail starting at the beginning.

In the Beginning:

The first line of any script should always look a bit like the top line in the example basic shell script; the only difference would be the path leading to the shell executable file, in this case the /bin/sh part. By default, the UNIX system will attempt to execute an ASCII text file

if the files name is supplied as the first argument on the command line. UNIX will attempt to execute the file in the current shell and try to process the included command strings within the file using the syntax rules of the current shell. So, if you are using the Bourne Shell as your default environment and the ASCII file contains a list of UNIX command structures formatted how the Bourne Shell likes them to be formatted, all will work fine. However, if you try and execute a C Shell file with a different syntax structure, the Operating System will complain about unrecognized commands and syntax errors. The reason is no one told the Operating System that it was a C Shell file, so it processed it as a Bourne Shell. The correct way to indicate this to the Operating System is to pass the script name to the shell executable as an argument thus:

user@system$ /bin/csh my_script arg_1 arg_2

However, it didn't take long for someone to notice that this was extra typing that could well be done away with and hence the short hand first line comment was born.

Basically, all comments in shell scripts start with a hash sign (#). However, for the first line only, UNIX reads past the hash to see what's next. If it finds an exclamation point (!), or Bang! as it's known, then what follows is taken as the path to the shell executable binary program. Not only can that, but all the command line arguments that the shell executable allows also be stacked up on this line. With this feature it doesn't matter what flavor of shell your environment is, you can execute a script in any other flavor of shell, as if it was a real UNIX command. Let’s look at the Bourne Shell syntax rules more closely. It would be helpful at this point to print out the sh man pages from your system so that you can compare them with what I have here.

Question 02: Invoking Shell Scripts

How do you invoke shell scripts?

A: There are two ways to invoke a shell script file.

1. Direct Interpretation:

In direct interpretation, the command csh filename [arg ...] invokes the program csh to interpret the script contained in the file filename.

2. Indirect Interpretation:

In indirect interpretation, we must insert as the first line of the file:

#! /bin/csh

or

#! /bin/csh -f

There are situations in which this is not necessary, but it won't hurt to have it and the file must be made executable using chmod. Then it can be invoked in the same way as any other command, i.e., by typing the script file name on the command line.

The -f option says that we want fast startup, which it will achieve by not reading or executing the commands in .cshrc. Thus for example, we won't have the set values for shell variables or the aliases from that file, but if we don't need them, this will be much faster (if we need a few of them, we can simply add them to the script file

Enjoying the preview?
Page 1 of 1