You are on page 1of 42

Unix And Shell Programming UNIT-III

GUDLAVALLERU ENGINEERING COLLEGE


(An Autonomous Institute with Permanent Affiliation to JNTUK,
Kakinada)
Seshadri Rao Knowledge Village, Gudlavalleru – 521 356.

Department of Information Technology(IT)

UNIX AND SHELL PROGRAMMING


(2018-19)

UNIT-III
Department Of Information Technology GEC Page 1
Unix And Shell Programming UNIT-III

 Introduction to Shell:

 The UNIX operating system is a complex collection of files and


programs. UNIX does not require any single method or interface.

 The shell was the interface to the operating system. It is a user


program that is ASCII based, that allows the user to specify
operations in a certain sequence.

 The user interacts with the system using a shell. A sequence of


operations can be scripted or automatic, by placing the
operations in a script file.

 A shell is a full featured programming language, with variables,


conditional statements, and the ability to execute other programs.
It can be, and is used to prototype new programs.

 Bourne shell:

 A Bourne shell (sh) is a UNIX shell or command


processor that is used for scripting. It was developed in
1977 by Stephen Bourne of AT&T and introduced in
UNIX Version 7.
 The Bourne shell is also known by its executable
program name, "sh" and the dollar symbol, "$," which is
used with command prompts.

 Shell Responsibities:

The shell provides an interface through which the user gives


input and the programs are executed based upon that. The
responsibilities of a Shell are:

Department Of Information Technology GEC Page 2


Unix And Shell Programming UNIT-III
Figure1:Shell Responsibilities

Program execution: The shell is responsible for the execution of


programs that is requested.

Each time you type in a line to the shell, the shell analyzes the line and
then determines what to do. As far as the shell is concerned, each line
follows the same basic format:

program-name arguments

The line that is typed to the shell is known more formally as


the command line. The shell scans this command line and determines
the name of the program to be executed and what arguments to pass to
the program.

The shell uses special characters to determine where the program name
starts and ends, and where each argument starts and ends. These
characters are collectively called whitespace characters, and are the
space character, the horizontal tab character, and the end-of-line
character, known more formally as the newline character. Multiple
occurrences of whitespace characters are simply ignored by the shell.
When you type the command

mv tmp/mazewars games

the shell scans the command line and takes everything from the start of
the line to the first whitespace character as the name of the program to
execute: mv. The set of characters up to the next whitespace character
is the first argument to mv: tmp/mazewars. The set of characters up to
the next whitespace character (known as a word to the shell) in this
case, the newline is the second argument to mv:games. After analyzing
the command line, the shell then proceeds to execute the mv command,
giving it the two arguments tmp/mazewars and games.

Figure 2: Execution of mv with two arguments

Variable and file name substitution: The shell allows us to assign


values to variables. When a variable is specified on the command line,
preceded by a dollar sign, the value that is assigned to that variable at
that point is substituted by the shell.

Department Of Information Technology GEC Page 3


Unix And Shell Programming UNIT-III

The shell also performs filename substitution on the command line. In


fact, the shell scans the command line looking for filename substitution
characters *, ?, or [...] before determining the name of the program to
execute and its arguments. Suppose that your current directory
contains the files as shown:

$ ls

mrs.todd

prog1

shortcut

sweeney

Now let's use filename substitution for the echo command:

$ echo * List all files

mrs.todd prog1 shortcut sweeney

How many arguments do you think were passed to the echo program,


one or four? Because we said that the shell is the one that performs the
filename substitution, the answer is four. When the shell analyzes the
line

echo *

it recognizes the special character * and substitutes on the command


line the names of all files in the current directory (it even alphabetizes
them for you):

echo mrs.todd prog1 shortcut sweeney

Then the shell determines the arguments to be passed to the command.


So echo never sees the asterisk. As far as it's concerned, four
arguments were typed on the command line

Department Of Information Technology GEC Page 4


Unix And Shell Programming UNIT-III

Figure 3:Execution of echo

I/O redirection: The shell is responsible for taking care of input and
output redirection on the command line.It scans the command line for
the occurrence of the special redirection characters <, >, or >>

When you type the command

echo Remember to tape Law and Order > reminder

the shell recognizes the special output redirection character > and takes


the next word on the command line as the name of the file that the
output is to be redirected to. In this case, the file is reminder.
Ifreminder already exists and you have write access to it, the previous
contents are lost (if you don't have write access to it, the shell gives you
an error message).

Before the shell starts execution of the desired program, it redirects the
standard output of the program to the indicated file. As far as the
program is concerned, it never knows that its output is being
redirected. It just goes about its merry way writing to standard output
(which is normally your terminal, you'll recall), unaware that the shell
has redirected it to a file.

Let's take another look at two nearly identical commands:

$ wc -l users

5 users

$ wc -l < users

In the first case, the shell analyzes the command line and determines
that the name of the program to execute is wc and it is to be passed two
arguments: -l and users

Department Of Information Technology GEC Page 5


Unix And Shell Programming UNIT-III

Figure 4: Execution of wc –l users

When wc begins execution, it sees that it was passed two arguments.


The first argument, -l, tells it to count the number of lines. The second
argument specifies the name of the file whose lines are to be counted.
So wc opens the file users, counts its lines, and then prints the count
together with the filename at the terminal.

Operation of wc in the second case is slightly different. The shell spots
the input redirection character ‘<’when it scans the command line. The
word that follows on the command line is the name of the file input is to
be redirected from. Having "gobbled up" the < users from the command
line, the shell then starts execution of the wc program, redirecting its
standard input from the file users and passing it the single argument -l

Pipeline hookup: The shell scans the command line for pipe character.
If it finds such characters, it connects the standard output from the
command preceding the | to the standard input of the one following the
|. It then initiates execution of both programs.

So when you type

who | wc -l

the shell finds the pipe symbol separating the commands who and wc.


It connects the standard output of the former command to the standard
input of the latter, and then initiates execution of both commands.
When the who command executes, it makes a list of who's logged in and
writes the results to standard output, unaware that this is not going to
the terminal but to another command instead.

When the wc command executes, it recognizes that no filename was


specified and counts the lines on standard input, unaware that
standard input is not coming from the terminal but from the output of
the who command.

Environmental control: The shell allows us to customize our


environment. Environment includes home directory, the list of
directories that is searched when a program is requested to be executed
and the characters that is prompted by the shell when the command is
typed.

Department Of Information Technology GEC Page 6


Unix And Shell Programming UNIT-III

Interpreted programming language: The shell has its own built in


programming language. The shell analyzes each statement in the
language one line at a time and then executes it.

 Meta Characters:

Metacharacters are special characters that are used to represent


something other than themselves . As a rule of thumb, characters
that are neither letters nor numbers may be metacharacters. Like
grep the shell has its own set of metacharacters, often called shell
wildcards . Shell metacharacters can be used to group commands
together, to abbreviate filenames and pathnames, to redirect and
pipe input/output, to place commands in the background, and so
forth.

Programs such as grep have a set of metacharacters, called


regular expression metacharacters , for pattern matching. These
should not be confused with shell metacharacters.

Metacharacter Purpose Example Meaning

$ Variable set Sets the variable


substitution name=Tom name to Tom ;
echo $name displays the value
Tom stored there.

! History !3 Re-executes the third


substitution event from the
history list.

* Filename rm * Removes all files.


substitution

? Filename ls ?? Lists all two-


substitution character files.

[] Filename cat f[123] Displays contents of


substitution f1 , f2 , f3 .

; Command ls;date;pwd Each command is


separator executed in turn .

& Background lp mbox& Printing is done in

Department Of Information Technology GEC Page 7


Unix And Shell Programming UNIT-III

Metacharacter Purpose Example Meaning

processing the background.


Prompt returns
immediately.

> Redirection of ls > file Redirects standard


output output to file .

< Redirection of ls < file Redirects standard


input input from file .

>& Redirection of ls >& file Redirects both


output and output and errors
error to file .

>! If no clobber ls >! file If file exists,


is set, override truncate and
it overwrite it, even if
noclobber is set.

>>! If no ls >>! file If file does not exist,


clobber is create it; even if
set, noclobber is set.
override it

Executes
commands and
Groups
sends output to
commands to
tmp file.
be executed in (ls ; pwd)
() a subshell >tmp

 Pipes and I/O redirections:


 A pipe is a form of redirection (transfer of standard output to
some other destination) that is used in Unix-like operating
systems to send the output of one command/program/process to
another command/program/process for further processing. The
Unix systems allow stdout of a command to be connected to stdin

Department Of Information Technology GEC Page 8


Unix And Shell Programming UNIT-III

of another command. You can make it do so by using the pipe


character ‘|’.
 Pipe is used to combine two or more command and in this the
output of one command act as input to another command and
this command output may act as input to next command and so
on. It can also be visualized as a temporary connection between
two or more commands/ programs/ processes. The command line
programs that do the further processing are referred to as filters.
 This direct connection between commands/ programs/ processes
allows them to operate simultaneously and permits data to be
transferred between them continuously rather than having to
pass it through temporary text files or through the display screen.

Pipes are unidirectional i.e data flow from left to right through


the pipeline.

Syntax :
command_1 | command_2 | command_3 | .... | command_N

The more command takes output of $ ls -l as its input. The effect of


this command is that the output of ls -l is displayed on screen at a
time. The pipe act as a container which take output of ls -l and giving
it to more as input. This command does not use a disk to connect
standard output of ls -l to standard input of more because pipe is
implemented in the main memory.

In terms of I/O redirection operators, the above command is


equivalent to the following command sequence.

$ ls -l -> temp

more -> temp (or more temp)

[contents of temp]

rm temp

2. Use cat, grep, tee and wc command to read particular entry from
user and store in a file and print line count.
$ cat result.txt | grep "Rajat Dua" | tee file2.txt | wc -l
This command select Rajat Dua and store them in file2.txt and print
total number of lines matching Rajat Dua.

Department Of Information Technology GEC Page 9


Unix And Shell Programming UNIT-III

3. Use ls and find to list and print all lines matching a particular
pattern in matching files.
$ ls -l | find ./ -type f -name "*.txt" -exec grep "program" {} \;
This command select files with .txt extension in the given directory
and search for pattern like “program” in the above example and print
those which have program in them.
4. Use head and tail to print lines in particular range in a file.
$ cat sample2.txt | head -7 | tail -5
This command select first 7 lines and last 5 lines from the file and
print those lines which are common to both of them.
I/O Redirection:
Redirection can be defined as changing the way from where commands
read input to where commands sends output. You can redirect input
and output of a command.

For redirection, meta characters are used. Redirection can be into


a file (shell meta characters are angle brackets '<', '>') or
a program (shell meta characters are pipe symbol '|').

Standard Streams In I/O Redirection

The bash shell has three standard streams in I/O redirection:

o standard input (stdin) : The stdin stream is numbered as stdin


(0). The bash shell takes input from stdin. By default, keyboard is
used as input.
o standard output (stdout) : The stdout stream is numbered as
stdout (1). The bash shell sends output to stdout. Output goes to
display.
o standard error (stderr) : The stderr stream is numbered as
stderr (2). The bash shell sends error message to stderr. Error
message goes to display.

Redirection Into A File

Each stream uses redirection commands. Single bracket '>' or double


bracket '>>' can be used to redirect standard output. If the target file
doesn't exist, a new file with the same name will be created.

Overwrite

Commands with a single bracket '>' overwrite existing file content.

> : standard output

Department Of Information Technology GEC Page 10


Unix And Shell Programming UNIT-III

< : standard input

2> : standard error

Note: Writing '1>' or '>' and '0<' or '<' is same thing. But for stderr


you have to write '2>'.

Syntax:

cat > <fileName>  

Example:

cat > sample.txt  

Append

Commands with a double bracket '>>' do not overwrite the existing


file content.

o >> - standard output


o << - standard input
o 2>> - standard error

Syntax:

cat >> <fileName>  

Example:

cat >> sample.txt  

Redirection Into a Program:

Pipe redirects a stream from one program to another. When pipe is


used to send standard output of one program to another program, first
program's data will not be displayed on the terminal, only the second
program's data will be displayed.

Although the functionality of pipe may look similar to that of '>' and '>>'
but has a significance difference. Pipe redirects data from one program
to another while brackets are only used in redirection of files.

Example:

ls *.txt | cat > txtFile  
 Shell Variables and Environment Variables:

Department Of Information Technology GEC Page 11


Unix And Shell Programming UNIT-III

Shell variables are variables that are contained exclusively within


the shell in which they were set or defined. They are often used to keep
track of ephemeral data, like the current working directory.

Environmental variables are variables that are defined for the current


shell and are inherited by any child shells or processes. Environmental
variables are used to pass information into processes that are spawned
from the shell.

By convention, these types of variables are usually defined using all


capital letters. This helps users distinguish environmental variables
within other contexts.

Printing Shell and Environmental Variables:


Each shell session keeps track of its own shell and environmental
variables. We can access these in a few different ways.
 env let's you modify the environment that programs run in by passing
a set of variable definitions into a command like this:
env VAR1="unix" command_to_run command_options
Since, as we learned above, child processes typically inherit the
environmental variables of the parent process, this gives you the
opportunity to override values or add additional variables for the child.
These show the environmental variables, but how do we see shell
variables?
The set command can be used for this. If we type set without any
additional parameters, we will get a list of all shell variables,
environmental variables, local variables, and shell functions:
Set BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:expand_aliases:extglob:e
xtquote:force_fignore:histappend:interactive_comments:logi
n_shell:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
This is usually a huge list. You probably want to pipe it into a pager
program to deal with the amount of output easily:
set | less
The amount of additional information that we receive back is a bit
overwhelming. We probably do not need to know all of the bash
functions that are defined.
Common Environmental and Shell Variables

Department Of Information Technology GEC Page 12


Unix And Shell Programming UNIT-III

Some environmental and shell variables are very useful and are
referenced fairly often.
Here are some common environmental variables that you will come
across:

 SHELL: This describes the shell that will be interpreting any


commands you type in. In most cases, this will be bash by
default, but other values can be set if you prefer other options.

 TERM: This specifies the type of terminal to emulate when


running the shell. Different hardware terminals can be emulated
for different operating requirements. You usually won't need to
worry about this though.

 USER: The current logged in user.

 PWD: The current working directory.

 OLDPWD: The previous working directory. This is kept by the


shell in order to switch back to your previous directory by
running cd -.

 LS_COLORS: This defines color codes that are used to optionally


add colored output to the lscommand. This is used to distinguish
different file types and provide more info to the user at a glance.

 MAIL: The path to the current user's mailbox.

 PATH: A list of directories that the system will check when looking
for commands. When a user types in a command, the system will
check directories in this order for the executable.

 LANG: The current language and localization settings, including


character encoding.

 HOME: The current user's home directory.

 _: The most recent previously executed command.

In addition to these environmental variables, some shell variables that


you'll often see are:

 BASHOPTS: The list of options that were used when bash was
executed. This can be useful for finding out if the shell
environment will operate in the way you want it to.

 BASH_VERSION: The version of bash being executed, in human-


readable form.

Department Of Information Technology GEC Page 13


Unix And Shell Programming UNIT-III

 BASH_VERSINFO: The version of bash, in machine-readable


output.

 COLUMNS: The number of columns wide that are being used to


draw output on the screen.

 DIRSTACK: The stack of directories that are available with


the pushd and popd commands.

 HISTFILESIZE: Number of lines of command history stored to a


file.

 HISTSIZE: Number of lines of command history allowed in


memory.

 HOSTNAME: The hostname of the computer at this time.

 IFS: The internal field separator to separate input on the


command line. By default, this is a space.

 PS1: The primary command prompt definition. This is used to


define what your prompt looks like when you start a shell
session. The PS2 is used to declare secondary prompts for when a
command spans multiple lines.

 SHELLOPTS: Shell options that can be set with the set option.

 UID: The UID of the current user.

Setting Shell and Environmental Variables


To better understand the difference between shell and environmental
variables, and to introduce the syntax for setting these variables, we will
do a small demonstration.
Creating Shell Variables
We will begin by defining a shell variable within our current session.
This is easy to accomplish; we only need to specify a name and a value.
We'll adhere to the convention of keeping all caps for the variable name,
and set it to a simple string.
TEST_VAR='Hello World!'
Here, we've used quotations since the value of our variable contains a
space. Furthermore, we've used single quotes because the exclamation
point is a special character in the bash shell that normally expands to
the bash history if it is not escaped or put into single quotes.
We now have a shell variable. This variable is available in our current
session, but will not be passed down to child processes.
We can see this by grepping new variable within the set output:

Department Of Information Technology GEC Page 14


Unix And Shell Programming UNIT-III

set | grep TEST_VAR


TEST_VAR='Hello World!'
Let's take this as an opportunity to demonstrate a way of accessing the
value of any shell or environmental variable.
echo $TEST_VAR
Hello World!
As you can see, reference the value of a variable by preceding it with
a $ sign. The shell takes this to mean that it should substitute the
value of the variable when it comes across this.
So now we have a shell variable. It shouldn't be passed on to any child
processes. We can spawn a newbash shell from within our current one
to demonstrate:
echo $TEST_VAR
If we type bash to spawn a child shell, and then try to access the
contents of the variable, nothing will be returned. This is what we
expected.
Get back to our original shell by typing exit:
Creating Environmental Variables
Now, let's turn our shell variable into an environmental variable. We
can do this by exporting the variable. The command to do so is
appropriately named:
export TEST_VAR
This will change our variable into an environmental variable. We can
check this by checking our environmental listing again:
printenv | grep TEST_VAR
TEST_VAR=Hello World!
This time, our variable shows up. Let's try our experiment with our
child shell again:
bash
echo $TEST_VAR
Hello World!
Great! Our child shell has received the variable set by its parent. Before
we exit this child shell, let's try to export another variable. We can set
environmental variables in a single step like this:
export NEW_VAR="Testing export"
Test that it's exported as an environmental variable:
printenv | grep NEW_VAR
NEW_VAR=Testing export
Now, let's exit back into our original shell:

Department Of Information Technology GEC Page 15


Unix And Shell Programming UNIT-III

exit
Let's see if our new variable is available:
echo $NEW_VAR
Nothing is returned.
This is because environmental variables are only passed to child
processes. There isn't a built-in way of setting environmental variables
of the parent shell. This is good in most cases and prevents programs
from affecting the operating environment from which they were called.
The NEW_VAR variable was set as an environmental variable in our
child shell. This variable would be available to itself and any of its child
shells and processes. When we exited back into our main shell, that
environment was destroyed.
Unsetting Variables
We still have our TEST_VAR variable defined as an environmental
variable. We can change it back into a shell variable by typing:
export -n TEST_VAR
It is no longer an environmental variable:
printenv | grep TEST_VAR
However, it is still a shell variable:
set | grep TEST_VAR
TEST_VAR='Hello World!'
If we want to completely unset a variable, either shell or environmental,
we can do so with the unsetcommand:
unset TEST_VAR
We can verify that it is no longer set:
echo $TEST_VAR
Nothing is returned because the variable has been unset
 Arthemetic Expressions:
Arithmetic Operators in Shell Programming are:

 Addition +

 Subtraction -

 Multiplication *

 Division /

 Modulus % to get remainder

expr command:

Department Of Information Technology GEC Page 16


Unix And Shell Programming UNIT-III

In shell script all variables hold string value even if they are
numbers. So, to perform arithmetic operations we use
the expr command.
The expr command can only work with integer values. For floating
point numbers we use the bc command.
To compute the result we enclose the expression in backticks ` `.
Addition
We use the + symbol to perform addition.
In the following example we are taking two integer value from the
user and showing the result after addition.
#!/bin/sh
# take two integers from the user
echo "Enter two integers: "
read a b
# perform addition
result=`expr $a + $b`
# show result
echo "Result: $result"
In the above script `expr $a + $b` means we are adding values
stored in variable a and band evaluating the expression using
the expr command. We are then saving the result of the addition
operation in the variable result.

Output:
$ sh add.sh
Enter two integers:
10 20
Result: 30
In the following example we are enclosing the variables in double
quotes and using bc to handle floating point numbers.
#!/bin/sh
# take two numbers from the user
echo "Enter two numbers: "
read a b
# perform addition
result=`expr "$a + $b" | bc`
# show result
echo "Result: $result"

Department Of Information Technology GEC Page 17


Unix And Shell Programming UNIT-III

Output:

$ sh add2.sh

Enter two numbers:

1.2 3.4

Result: 4.6

Subtraction:
To perform subtraction we use the  “–“ symbol.
In the following example we will take two numbers from the user and
print the subtraction result.
#!/bin/sh
# take two numbers from user
echo "Enter two numbers: "
read a b
# compute subtraction result
result=`expr "$a - $b" | bc`
# print output
echo "Result: $result"
Output:

$ sh subtract.sh

Enter two numbers:

10 9

Result: 1

$ sh subtract.sh

Enter two numbers:

9 10

Result: -1

Department Of Information Technology GEC Page 18


Unix And Shell Programming UNIT-III

$ sh subtract.sh

Enter two numbers:

10.5 9.1

Result: 1.4

Multiplication:
To perform multiplication we use the * symbol.
In the following example we will multiply two numbers.
#!/bin/sh
# take two numbers from user
echo "Enter two numbers: "
read a b
# compute multiplication result
result=`expr "$a * $b" | bc`
# print output
echo "Result: $result"

Output:

$ sh multiplication.sh

Enter two numbers:

23

Result: 6

$ sh multiplication.sh

Enter two numbers:

-2 3

Result: -6

$ sh multiplication.sh

Department Of Information Technology GEC Page 19


Unix And Shell Programming UNIT-III

Enter two numbers:

1.5 4

Result: 6.0

Division:
To perform division we use the / symbol.
In the following example we will divide two numbers.
#!/bin/sh
# take two numbers from user
echo "Enter two numbers: "
read a b
# compute division result
result=`expr "$a / $b" | bc -l`
# print output
echo "Result: $result"
Output:
$ sh division.sh
Enter two numbers:
42
Result: 2
$ sh division.sh
Enter two numbers:
52
Result: 2.50000000000000000000
The -l option loads the standard math library with default scale set
to 20.
Modulus:
To perform modulus operations we use the % symbol.
In the following example we will get the remainder by dividing two
numbers.
#!/bin/sh
# take two numbers from user
echo "Enter two numbers: "
read a b
# compute modulus result
result=`expr "$a % $b" | bc`

Department Of Information Technology GEC Page 20


Unix And Shell Programming UNIT-III

# print output
echo "Result: $result"
Output:

$ sh modulus.sh

Enter two numbers:

52

Result: 1

$ sh modulus.sh

Enter two numbers:

5.1 2

Result: 1.1

 Conditional Expressions:

If statements allow us to make decisions in our Bash scripts. They


allow us to decide whether or not to run a piece of code based upon
conditions that we may set. If statements, combined with loops allow
us to make much more complex scripts which may solve larger
tasks.

A basic if statement effectively says, if a particular test is true, then


perform a given set of actions. If it is not true then don't perform
those actions. If follows the format below:

if [ <some test> ]
then
<commands>
fi

Anything between then and fi (if backwards) will be executed only if the


test (between the square brackets) is true.

Let's look at a simple example:

Department Of Information Technology GEC Page 21


Unix And Shell Programming UNIT-III

if_example.sh

#!/bin/bash

# Basic if statement

if [ $1 -gt 100 ]

then

echo Hey that\'s a large number.

pwd

fi

date

Output:

./if_example.sh 15

Sat 11 Aug 23:37:22 2018

./if_example.sh 150

Hey that's a large number.

/home/ryan/bin

Sat 11 Aug 23:37:22 2018

Test:

The square brackets ( [ ] ) in the if statement above are actually a


reference to the command test. This means that all of the operators that
test allows may be used here as well.

Operator Description

!EXPRESSION The EXPRESSION is false.

The length of STRING is greater than


-n STRING
zero.
The lengh of STRING is zero (ie it is
-z STRING
empty).
STRING1 = STRING1 is equal to STRING2

Department Of Information Technology GEC Page 22


Unix And Shell Programming UNIT-III

STRING2
STRING1 !=
STRING1 is not equal to STRING2
STRING2
INTEGER1
INTEGER1 is numerically equal to
-eq
INTEGER2
INTEGER2
INTEGER1
INTEGER1 is numerically greater than
-gt
INTEGER2
INTEGER2
INTEGER1
INTEGER1 is numerically less than
-lt
INTEGER2
INTEGER2
-d FILE FILE exists and is a directory.
-e FILE FILE exists.
FILE exists and the read permission is
-r FILE
granted.
FILE exists and it's size is greater than
-s FILE
zero (ie. it is not empty).
FILE exists and the write permission is
-w FILE
granted.
FILE exists and the execute permission
-x FILE
is granted.

A few points to note:

 = is slightly different to -eq. [ 001 = 1 ] will return false as = does


a string comparison (ie. character for character the same)
whereas -eq does a numerical comparison meaning [ 001 -eq 1 ]
will return true.

 When we refer to FILE above we are actually meaning a path.


Remember that a path may be absolute or relative and may refer
to a file or a directory.

 Because [ ] is just a reference to the command test we may


experiment and trouble shoot with test on the command line to
make sure our understanding of its behaviour is correct.

test 001 = 1

Department Of Information Technology GEC Page 23


Unix And Shell Programming UNIT-III

echo $?

test 001 -eq 1

echo $?

touch myfile

test -s myfile

echo $?

ls /etc > myfile

test -s myfile

echo $?

Let's break it down:

 Line 1 - Perform a string based comparison. Test doesn't print the
result so instead we check it's exit status which is what we will do
on the next line.

 Line 2 - The variable $? holds the exit status of the previously run
command (in this case test). 0 means TRUE (or success). 1 =
FALSE (or failure).

 Line 4 - This time we are performing a numerical comparison.

 Line 7 - Create a new blank file myfile (assuming that myfile


doesn't already exist).

 Line 8 - Is the size of myfile greater than zero?

 Line 11 - Redirect some content into myfile so it's size is greater


than zero.

 Line 12 - Test the size of myfile again. This time it is TRUE.

Department Of Information Technology GEC Page 24


Unix And Shell Programming UNIT-III

Nested If statement:

It is also possible to have an if statement inside of another if statement.


For example, we may want to analyse a number given on the command
line like so:

nested_if.sh

#!/bin/bash

# Nested if statements

if [ $1 -gt 100 ]

then

echo Hey that's a large number.

if (( $1 % 2 == 0 ))

then

echo And is also an even number.

fi

fi

Let's break it down:

 Line 4 - Perform the following, only if the first command line
argument is greater than 100.

 Line 8 - This is a light variation on the if statement. If we would


like to check an expression then we may use the double brackets
just like we did for variables.

 Line 10 - Only gets run if both if statements are true.

If Elif Else

Sometimes we may have a series of conditions that may lead to different


paths.

if [ <some test> ]
then
<commands>
elif [ <some test> ] 

Department Of Information Technology GEC Page 25


Unix And Shell Programming UNIT-III

then
<different commands>
else
<other commands>
fi

For example it may be the case that if you are 18 or over you may go to
the party. If you aren't but you have a letter from your parents you may
go but must be back before midnight. Otherwise you cannot go.

if_elif.sh

#!/bin/bash

# elif statements

if [ $1 -ge 18 ]

then

echo You may go to the party.

elif [ $2 == 'yes' ]

then

echo You may go to the party but be back


before midnight.

else

echo You may not go to the party.

fi

You can have as many elif branches as you like. The final else is also
optional.

Boolean Operations

Sometimes we only want to do something if multiple conditions are met.


Other times we would like to perform the action if one of several
condition is met. We can accommodate these with boolean operators.

 and - &&

Department Of Information Technology GEC Page 26


Unix And Shell Programming UNIT-III

 or - ||

For instance maybe we only want to perform an operation if the file is


readable and has a size greater than zero.

and.sh

#!/bin/bash

# and example

if [ -r $1 ] && [ -s $1 ]

then

echo This file is useful.

fi

Maybe we would like to perform something slightly different if the user


is either bob or andy.

#!/bin/bash

# or

if [ $USER == 'bob' ] || [ $USER == 'andy' ]

then

ls -alh

else

ls

fi

Case Statements:

Sometimes we may wish to take different paths based upon a variable


matching a series of patterns. We could use a series of if and elif
statements but that would soon grow. Fortunately there is
a case statement which can make things clear. It's a little hard to
explain so here are some examples to illustrate:

Department Of Information Technology GEC Page 27


Unix And Shell Programming UNIT-III

case <variable> in
<pattern 1>)
<commands>
;;
<pattern 2>)
<other commands>
;;
esac

Here is a basic example:

case.sh

#!/bin/bash

# case example

case $1 in

start)

echo starting

;;

stop)

echo stoping

;;

restart)

echo restarting

;;

*)

echo don\'t know

;;

esac

Let's break it down:

Department Of Information Technology GEC Page 28


Unix And Shell Programming UNIT-III

 Line 4 - This line begins the case mechanism.

 Line 5 - If $1 is equal to 'start' then perform the subsequent


actions. the ) signifies the end of the pattern.

 Line 7 - We identify the end of this set of statements with a


double semi-colon ( ;; ). Following this is the next case to
consider.

 Line 14 - Remember that the test for each case is a pattern.
The * represents any number of any character. It is essentially a
catch all if for if none of the other cases match. It is not necessary
but is often used.

 Line 17 - esac is case backwards and indicates we are at the end
of the case statement. Any other statements after this will be
executed normally.

./case.sh start

starting

./case.sh restart

restarting

./case.sh blah

don't know

 Control Structures:

Sometimes we want to run a command (or group of commands) over


and over. This is called iteration, repetition, or looping.

for loop:

The most commonly used shell repetition structure is the for loop,
which has the general form: for variable in list do command(s) done.

Here is a simple example:

#!/bin/sh

for host in $*

do

Department Of Information Technology GEC Page 29


Unix And Shell Programming UNIT-III

ping $host

done

while loop:

The general form of the while loop is: while condition do command(s)
done As long as the condition is true, the commands between the do
and the done are executed.

Example #!/bin/sh

# Print a message ten times count=10

while test $count -gt 0

do

echo $*

count=‘expr $count - 1‘

done

until loop:

Another kind of iteration structure is the until loop. It has the general
form:

until condition

do

command(s)

done

This loop continues to execute the command(s) between the do and


done until the condition is true.

We can rewrite the previous script using an until loop instead of the
while loop:

#!/bin/sh

Department Of Information Technology GEC Page 30


Unix And Shell Programming UNIT-III

# Print a message ten times

count=10

until test $count -eq 0

do

echo $*

count=‘expr $count - 1‘

done

 Positional Parameters:

A positional parameter is a variable within a shell program its value


is set from an argument specified on the command line that invokes
the program. Positional parameters are numbered and are referred to
with a preceding ``$'': $1, $2, $3, and so on.
A shell program may reference up to nine positional parameters. If a
shell program is invoked with a command line that appears like this:
shell prog pp1 pp2 pp3 pp4 pp5 pp6 pp7 pp8 pp9

then positional parameter $1 within the program is assigned the


value pp1, positional parameter $2 within the program is assigned
the value pp2, and so on, at the time the shell program is invoked.

To practice positional parameter substitution, create a file


called pp (short for positional parameters). (Remember, the directory
in which these example files reside must be in $PATH.) Then enter
the echocommands shown in the following screen. Enter the
command lines so that running the cat command on your completed
file will produce the following output:
$ cat pp

echo The first positional parameter is: $1

echo The second positional parameter is: $2

echo The third positional parameter is: $3

echo The fourth positional parameter is: $4

Department Of Information Technology GEC Page 31


Unix And Shell Programming UNIT-III

If you execute this shell program with the arguments one, two, three,


and four, you will obtain the following results (but first you must
make the shell program pp executable using the chmod command):

$ chmod u+x pp

$ set one two three four

The first positional parameter is: one

The second positional parameter is: two

The third positional parameter is: three

The fourth positional parameter is: four

The who command lists all users currently logged in on the system.


How can you make a simple shell program called whoson, that will
tell you if the owner of a particular login is currently working on the
system?

Type the following command line into a file called whoson:


who | grep $1

The who command lists all current system users, and grep searches


that output for a line with the string contained as a value in the
positional parameter $1.

Now try using your login as the argument for the new
program whoson. For example, suppose your login is sue. When you
issue the whoson command, the shell program substitutes sue for
the parameter $1in your program and executes as if it were:
who | grep sue

The output appears on your screen as follows:

$ whoson sue

sue tty26 Jan 24 13:35

 Shell Commands:

1. set command:

Department Of Information Technology GEC Page 32


Unix And Shell Programming UNIT-III

set is a built-in function of the Bourne shell(sh), C shell (csh),


and Korn shell (ksh), which is used to define and determine the
values of the system environment.
set syntax
Bourne shell (sh):

set [--aefhkntuvx[argument]]...

Options: Bourne Shell (sh)


-e Exit immediately if a command exits with a non-zero exit
status.

-f Disable file name generation

-n Read commands but do not execute them.

-t Exit after reading and executing one command.

-u Treat unset variables as an error when substituting.

-v Print shell input lines as they are read.

-x Print commands and their arguments as they are executed.

In sh, the set built-in command has the following options:

Using + rather than - causes these flags to be turned off. These flags


can also be used upon invocation of the shell itself. The current set
of flags may be found in the variable $-. The remaining arguments
are positional parameters and are assigned, in order, to $1, $2, etc.
If no arguments are given the values of all names are printed.

For each name, the unset command removes the


corresponding variable or function value. The special
variables PATH, PS1, PS2, MAILCHECK, and IF cannot be unset.

With the export built-in command, the given names are


marked for automatic export to the environment of subsequently
executed commands. If no arguments are given, variable names that
have been marked for export during the current shell's execution
are listed. Function names are not exported.

Null Command:

Bash and sh both use colons (":") in more than one context. You'll
see it used as a separator ($PATH, for example), as a modifier ($
{n:="foo"}) and as a null operator ("while :").

You'll also see the null operator usage in "if-then" sections:

if [ "$T1" = "$T2" ]

Department Of Information Technology GEC Page 33


Unix And Shell Programming UNIT-III

then
:
else
echo "Nope"
fi

export command:
export command is used to export a variable or function to
the environment of all the child processes running in the current
shell.
export varname=value
export -f functionname # exports a function in the current shell.

It exports a variable or function with a value. “env” command lists


all the environment variables. In the following example, you can see
that env displays the exported variable.
$ export country=India
$ env
SESSIONNAME=Console
country=India
_=/usr/bin/env

“export -p” command also displays all the exported variable in the
current shell.

Shift command:
shift command is used to shift the positional parameters left by N
number of times and renames the variable accordingly after
shifting.
$ cat shift.sh
#! /bin/bash

while [ $# -gt 0 ]
do
case "$1" in

-l) echo "List command"


ls
shift
;;
-p) echo "Process command"
ps -a

Department Of Information Technology GEC Page 34


Unix And Shell Programming UNIT-III

shift
;;
-t) echo "Hash Table command"
hash
shift
;;
-h) echo "Help command"
help
shift
;;
esac
done

$./shift.sh -l -t
List command analysis break testing t1.sh temp Hash Table
command
hits command
1 /usr/bin/ls

example-shell script:
For our first shell script, we'll just write a script which says "Hello
World". We will then try to get more out of a Hello World program than
any other tutorial you've ever read :-) 
Create a file (first.sh) as follows:
#!/bin/sh
# This is a comment!
echo Hello World # This is a comment, too!

The first line tells Unix that the file is to be executed by /bin/sh. This is
the standard location of the Bourne shell on just about every Unix
system. If you're using GNU/Linux, /bin/sh is normally a symbolic link
to bash (or, more recently, dash).

The second line begins with a special symbol: #. This marks the line as
a comment, and it is ignored completely by the shell. 
The third line runs a command: echo, with two parameters, or
arguments - the first is "Hello"; the second is "World". 
Note that echo will automatically put a single space between its
parameters. 
The # symbol still marks a comment; the # and anything following it is
ignored by the shell.

Department Of Information Technology GEC Page 35


Unix And Shell Programming UNIT-III

now run chmod 755 first.sh to make the text file executable, and
run ./first.sh. 
Your screen should then look like this:
$ chmod 755 first.sh
$ ./first.sh
Hello World
$
You could even just run:
$ echo Hello World
Hello World
$

Programs:

1. Write a shell script to print the factorial of first n natural numbers

echo "enter range"


read n
i=1
while [ $i -le $n ]
do
j=1 fact=1
while [ $j -le $i ]
do
fact=`expr $fact \* $j`
j=`expr $j + 1`
done
echo factorial of $i is: $fact
i=`expr $i + 1`
done

2. Write a shell script to generate a multiplication table of the given


number
echo "Enter a Number"
read n
i=0
while [ $i -le 10 ]
do
    echo " $n x $i = `expr $n \* $i`"
    i=`expr $i + 1`
done

Department Of Information Technology GEC Page 36


Unix And Shell Programming UNIT-III

3. Write a shell script that counts the number of lines and words
present in a given file.
echo Enter the filename
read file
w=`cat $file | wc -w`
c=`cat $file | wc -c`
l=`cat $file | wc -l` #l=`grep -c "." $file`
echo Number of characters in $file is $c
echo Number of words in $file is $w
echo Number of lines in $file is $l

4.Write a shell script that displays the list of all files in the given
directory

echo directory

read d

for file in $d

do

if [ -d $file ]

then

ls $file

fi

done

5. Write a shell script (small calculator) that adds, subtracts, multiplies


and divides the given two integers. There are two division options: one
returns the quotient and the other returns reminder. The script requires
3 arguments: The operation to be used and two integer numbers. The
options are add (-a), subtract (-s), multiply (-m), quotient (-c) and
reminder (-r).

echo "enter first value"


read x
echo "Enter Second Value "
read y
while [ ${q:-1} -ne 0 ]
do

Department Of Information Technology GEC Page 37


Unix And Shell Programming UNIT-III

echo "Enter -a for adding"


echo "Enter -s for subtraction"
echo "Enter -m for multiplication"
echo "Enter -c for Quotient"
echo "Enter -r for reminder"
echo choice
read b
case $b in
-a) p=`expr $x + $y`
echo "Sum = $p"
;;
-s) p=`expr $x - $y`
echo "difference = $p"

;;
-m) p=`expr $x \* $y`
echo "Product = $p"
;;
-c) p=`expr $x / $y`
echo "quotient = $p"
;;
-r) p=`expr $x % $y`
echo "reminder = $p"
;;
*) echo "none"
break
;;
esac
done

Assignment-Cum-Tutorial Questions
A. Questions testing the understanding / remembering level of
students
I) Objective Questions

1. What is a shell script? []


a) group of commands
b) a file containing special symbols

Department Of Information Technology GEC Page 38


Unix And Shell Programming UNIT-III

c) a file containing a series of commands


d) group of functions

2.Shell scripts need to be saved with an extension .sh . []


a) True
b) False

3. Shell scripts are executed in a separate child shell process. []


a) True
b) False

4. The first line in any shell script begins with a _____ []


a) &
b) !
c) $
d) #

5.What are positional parameters? []


a) special variables for assigning arguments from the command line
b)pattern matching parameters
c) special variables for reading user input
d) special variables and patterns

6. Which UNIX operating system command would you use to display


the shell's environment variables?

7.Which of the following special shell variables is used to process


number of the last background job? []
a) $!
b) $#
c) $0
d) $*

8. We can use test to test various file attributes. []


a)True
b) False

II. Descriptive Questions:


1. Define shell and describe the responsibilities of shell?

Department Of Information Technology GEC Page 39


Unix And Shell Programming UNIT-III

2. Define metacharacter? List and explain about any 4


metacharacters available in unix.
3. Illustrate the difference between echo “$SHELL” and echo
‘$SHELL’.
4. Explain the following:
a. Shell Variables
b. Shell Environment Variables
5. Illustrate the significance of following UNIX system variables:
HOME, PS1, PS2, PATH, TERM, MAILCHECK.
6. Describe user defined variables and system/Environment
variables.
7. Demonstrate Shell Commands with examples.
8. Explain about different conditional expressions available in
Bourne shell.
9. Briefly write a note on pipes and I/O Redirections.
10.Define standard input, standard output and standard error.
11. Demonstrate Input/Output Redirection.
12. Name the three sources and destinations of standard input and
standard output.
13. Describe Arthimetic expressions with examples.
14. Demonstrate conditional expressions with examples.
15. Explain the significance of test command.
16. Briefly explain positional parameters.
17. Write a shell script which gets executed the moment the user logs
in. It should display the message “good morning/good
afternoon/good evening” depending upon the time at which the user
logs in.
18. Write a shell script to print the factorial of first n natural
numbers.
19. Write a shell script to generate a multiplication table of the given
number.
20. Write a shell script that counts the number of lines and words
present in a given file.
21. Write a shell script that displays the list of all files in the given
directory.
22. Write a shell script (small calculator) that adds, subtracts,
multiplies and divides the given two integers. There are two division
options: one returns the quotient and the other returns reminder.
The script requires 3 arguments:The operation to be used and two

Department Of Information Technology GEC Page 40


Unix And Shell Programming UNIT-III

integer numbers. The options are add (-a), subtract (-s), multiply (-
m), quotient (-c) and reminder (-r).

B.GATE Questions:
1. Which of the following shell scripts will produce the output “my first
script” ? []
a) for i in my first script { echo –i $i}
b) for my first script; do echo –n; done
c) for i in my first script; do echo –i $i; done
d) for n in my first script; do echo –i $i; done

2. Which one of the following options is not a shell in UNIX system?


[]
a) Bourne Shell
b) C Shell
c) Net Shell
d) Korn Shell

3.Which of the following displays the exit status of the last executed
command?
[]
a) echo $4
b) echo $$
c) echo $?
d) echo $!

Department Of Information Technology GEC Page 41


Unix And Shell Programming UNIT-III

Department Of Information Technology GEC Page 42

You might also like