You are on page 1of 6

Unix Shell Programming

System files in Unix


There are two files which are responsible to the settings of Unix environment
•.Login
is to set conditions which will apply to the whole session and to perform actions that are
relevant only at login.
•.profile
is used to set conditions and perform actions specific to the shell and to each invocation of it.
Used to set standard shell variables to set the shell environment.

Shell Variables –
The variable is a value that can be changed according to the requirement of user.
•User created shell variables- we can type name of variable with value
e.g. $ n=0 etc.
2. Standard shell variable or System shell variable - the shell maintains its own set of shell
variables. Some times they are called as keyword variables. Most of the keyword variables
are either inherited by the shell when it is started up. You can assign new values to these
variables from the command line or from the .profile file in your home directory.
these variables can be see through – set command
$ set

HOME – display name of your home directory.


$ HOME
PATH – when user give the shell an absolute or relative pathname that is not just a simple file name as
command, it looks in the specified directory. The PATH variable specifies the directories in the order
the shell is to search them.
e.g - $ PATH = /bin :/usr/bin : /usr/students/bin :/
IFS – called as Inter Field Separator. Set of characters that are used to separate words in a command
Line.
e.g- space, tabs, ;, : | etc.
PS1 – called as primary prompt. This is a symbol used by the shell as prompt. Like $, % etc.
user can change this prompt by using following sequence –
$ PS1= “Alok >”
PS2 - Secondary prompt. This is given when Unix thinks you have started a new line without finishing
a command. You can continue a line by using a “\”
SHELL – path of shell ( e.g - /bin/sh)
MAIL – pathname of the file contains user’s mails.

SHELL PROGRAMMING
There are three king of construct in any programming language
•Sequential
•Conditional
•Iterative
•Sequential – where each statement execute sequentially i.e. each statement depends on the
previous statement.
e.g. write a shell script to calculate sum of two no.s

echo “enter 2 nos :”


read a
read b
c=`expr $a + $b`
echo $c

Integer Arithmetic
there are following commands which are used for Integer arithmetics –

•expr – it is used to make any kind of arithmetic expression.


e.g.
a=`expr $b + $c`
a=`expr $b - $c`
a=`expr $b “*” $c` or a=`expr $b \* $c`
a=`expr $b \/ $c` or a=`expr $b “\” $c`
a=`expr $b \% $c` or a=`expr $b “%” $c`

Conditional Constructs –

If then else –
If [ condition ]
then
------
else
---------
fi
e.g. write a shell script to find max among 2 nos.
echo “enter 2 nos:”
read a
read b
If [ $a –gt $b]
then
echo “$a is greater”

else
echo “$b is greater”
fi

Switch case –
case $variable
•statement1 ;;
•Statement2 ;;
•Statement3 ;;
•--------
esac

e.g. Write a Shell script to enter choice if choice is + then add 2 nos, - then subtarct etc.
echo “enter 2 nos :”
read a
read b
echo “enter choice : + - * or /”
read ch

case $ch
‘+’) c=`expr $a + $b`
echo $c ;;
‘-’) c=`expr $a - $b`
echo $c;;
esac

Switches with IF command


-lt <
-gt >
-le <=
-ge >=
-eq =
-ne !=
-a AND
-o OR
-r read permission
-w Write Permission

-f ordinary file
-d Directory file

If with “test” command –


If `test <condition>`
then
---------
fi

e.g. – write a shell script to check whether age is greater than 18 or no


read age
If `test $age –gt 18`
then
echo “age is greater”
else
echo “age is less”
fi

•write a shell script to check whether a particular file has read permission or not if not then assign
read permission.

echo “enter name of file “


read file
if [ -r $file ]
then
echo “has read permission”
else
chmod u+r $file
fi

2. write a program to check whether file is ordinary or directory

echo “enter file name :”


read file
if [ -f $file ]
then
echo “ordinary”
fi
if [ -d $file]
then
echo “directory”
fi

Assignment

•Write a program to find max no among 3 nos


•Write a program calculate simple interest
•Write a program to check whether no is even or odd

3. Iterative Constructs – are the programming techniques where a particular operations needs
to repeat several time to find a particular result.

e.g. table of a no, factorial, series generation etc.

There are 3 parts of any iteration


•Start or initialization
•Stop
•Increment or decrement
while –
while [ cond ]
do
-----
done
e.g. Write a shell script to display 0 to 10 even nos
i=0 # intialization or start
while [ $i –le 10 ] # stop
do
echo $i
i=`expr $i + 2` # increment
done

•Write a program to display odd nos from 20 to 1


i=20 #start
while [ $i –ge 20 ] #stop
do
i=`expr $i -2` # decrement
echo $i
done
•Write program to display table of a no.
•Write program to calculate factorial of a no
•Write a program to check whether no is prime or not

Command Line Arguments –


All the words written at the command prompt are called as command line arguments.
e.g 1
$ myshell foo bar

First argument $0 myshell


Second $1 foo
Third $2 bar etc.

There are certain predefined a arguments –


1. $1, $2 -------------$3 individual value of arguments
•$# total no. of arguments
•$* or $@ value of all the arguments at command line

e.g. echo "Total number of command line argument are in e.g. 1 $# $@"
echo "$0 is script name"
echo "$1 is first argument"
echo "$2 is second argument"
echo "All of them are :- $* or $@"
$# - 3, $0 – myfile, $1 – foo , $2 – bar , $@=3

Problems –
•Write a shell script with the help of command line arguments to check whether no. is even or
odd.
Vi even

C=`expr $1 \% 2`
If [ $c = 0 ]
then
echo “no is even”
Else
echo “no is odd”
Fi

2. Write a shell script using command line arguments to check whether no. is prime or not.
3. Calculate factorial of a no.

for loop –
for <var> in <range>
do
------------
done

e.g. for I in 1 2 3 4 5
do
echo $i
done

for loop with command Line arguments –

For I in $* # loop will take all the arguments as range


Do
echo $i
Done

$sh data a b c # $* will replace with a b c


Ouput will be -abc

1. Write a shell script to check whether following patterns are in data file or not.

Vi file

for j in $* # $* = is that ok
do
grep “$j” data
done

$sh file is that ok

String Handling –

•Calculate the length of a string

a=“bombay”
c=`expr “$a” : ‘.*’`
echo $c # will display 6

2. Display substring from a string

a=“bombay”
c=`expr “$a” : ‘..\(..\)’`
echo $c # will display “mbay”

You might also like