Gudlavalleru Engineering College: Seshadri Rao Knowledge Village, Gudlavalleru - 521 356
Gudlavalleru Engineering College: Seshadri Rao Knowledge Village, Gudlavalleru - 521 356
Gudlavalleru Engineering College: Seshadri Rao Knowledge Village, Gudlavalleru - 521 356
UNIT-III
Department Of Information Technology GEC Page 1
Unix And Shell Programming UNIT-III
Introduction to Shell:
Bourne shell:
Shell Responsibities:
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 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.
$ ls
mrs.todd
prog1
shortcut
sweeney
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 >>
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.
$ 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
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.
who | wc -l
Meta Characters:
Executes
commands and
Groups
sends output to
commands to
tmp file.
be executed in (ls ; pwd)
() a subshell >tmp
Syntax :
command_1 | command_2 | command_3 | .... | command_N
$ ls -l -> 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.
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.
Overwrite
Syntax:
cat > <fileName>
Example:
cat > sample.txt
Append
Syntax:
cat >> <fileName>
Example:
cat >> sample.txt
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:
Some environmental and shell variables are very useful and are
referenced fairly often.
Here are some common environmental variables that you will come
across:
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.
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.
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 /
expr command:
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"
Output:
$ sh add2.sh
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
10 9
Result: 1
$ sh subtract.sh
9 10
Result: -1
$ sh subtract.sh
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
23
Result: 6
$ sh multiplication.sh
-2 3
Result: -6
$ sh multiplication.sh
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`
# print output
echo "Result: $result"
Output:
$ sh modulus.sh
52
Result: 1
$ sh modulus.sh
5.1 2
Result: 1.1
Conditional Expressions:
if [ <some test> ]
then
<commands>
fi
if_example.sh
#!/bin/bash
# Basic if statement
if [ $1 -gt 100 ]
then
pwd
fi
date
Output:
./if_example.sh 15
./if_example.sh 150
/home/ryan/bin
Test:
Operator Description
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.
test 001 = 1
echo $?
echo $?
touch myfile
test -s myfile
echo $?
test -s myfile
echo $?
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).
Nested If statement:
nested_if.sh
#!/bin/bash
# Nested if statements
if [ $1 -gt 100 ]
then
if (( $1 % 2 == 0 ))
then
fi
fi
Line 4 - Perform the following, only if the first command line
argument is greater than 100.
If Elif Else
if [ <some test> ]
then
<commands>
elif [ <some test> ]
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
elif [ $2 == 'yes' ]
then
else
fi
You can have as many elif branches as you like. The final else is also
optional.
Boolean Operations
and - &&
or - ||
and.sh
#!/bin/bash
# and example
if [ -r $1 ] && [ -s $1 ]
then
fi
#!/bin/bash
# or
then
ls -alh
else
ls
fi
Case Statements:
case <variable> in
<pattern 1>)
<commands>
;;
<pattern 2>)
<other commands>
;;
esac
case.sh
#!/bin/bash
# case example
case $1 in
start)
echo starting
;;
stop)
echo stoping
;;
restart)
echo restarting
;;
*)
;;
esac
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:
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.
#!/bin/sh
for host in $*
do
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
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
We can rewrite the previous script using an until loop instead of the
while loop:
#!/bin/sh
count=10
do
echo $*
count=‘expr $count - 1‘
done
Positional Parameters:
$ chmod u+x pp
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
$ whoson sue
Shell Commands:
1. set command:
set [--aefhkntuvx[argument]]...
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 :").
if [ "$T1" = "$T2" ]
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.
“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
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.
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:
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
;;
-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
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
3.Which of the following displays the exit status of the last executed
command?
[]
a) echo $4
b) echo $$
c) echo $?
d) echo $!