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

Shellprog

Uploaded by

sainiguharoy25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Shellprog

Uploaded by

sainiguharoy25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

Unix Shell Programs

• Argument passing
• Decision making
• Flow control
• Data input and output
Comments

 Comments in Shell Programs


Comments into shell programs are
inserted by using the pound sign
(#)
All text to the end of the line as a
comment
Arithmetic on Shell
Variables

Shell variables are always stored as


characters. To do arithmetic on shell
variables, use the expr command
general form of the command is
expr integer operator integer
Arithmetic on Shell
Variables

 + Adds the two integers.


- Subtracts the second integer from the
first.
* Multiplies the two integers.
/ Divides the first integer by the second.
% Gives the modulus (remainder) of the
division.
Note: avoid using the asterisk operator (*)
alone for multiplication. Use (\*).
Arithmetic on Shell
Variables
Parentheses are not recognized by
expr, so to override the precedence,
you may use back quotation marks
to change the precedence
Eg. $ expr 5 + 7 / 3
7
Eg. $expr 'expr 5 + 7' / 3
4
Passing Arguments to
Shell Programs
 Paramters are positional and denoted by
$0 $1 … $9 for nine parameters.
Use $* variable to pass any number of
arguments to the program
OR
Use shift to pass any number of arguments
to the program
$0 is the program name
$# Gives the number of arguments
Decision Making

if command_1 then
command_2
command_3
fi
command_4
Decision Making

if command_1 then
command_2
command_3
else
command_4
command_5
fi
You can also use elif eqv to else followed by if
Data Output

Use the echo command


echo command recognizes several special
escape characters that assist in
formatting
output Some of them are
\b Backspace
\c Prints line without newline character
\n Newline
\t Tab
Testing Conditions with
test
General form of the command is as follows: test condition
conditions that can be tested are
1) String operators that test the condition or relationship of
character strings
2) Integer relationships that test the numerical relationship of
two integers
3) File operators that test for the existence or state of a file
4) Logical operators that allow for and/or combinations of the
other conditions.

Note:
Zero exit status if the condition is true and a nonzero exit
status if the condition is false
String Operators for test

str1
True if str1 is the same length and
=
contains the same characters as str2
str2
str1 !
= True if str1 is not the same as str2
str2
-n True if the length of str1 is greater than
str1 0 (is not null)
-z
True if str1 is null (has a length of 0)
str1
str1 True if str1 is not null
Numeric Operators for
test
int1 -eq int2 True if int1 is numerically equal to int2

int1 -ne int2 True if int1 is not equal to int2

int1 -gt int2 True if int1 is greater than int2

int1 -ge int2 True if int1 is greater than or equal to int2

int1 -lt int2 True if int1 is less than int2

int1 -le int2 True if int1 is less than or equal to int2


File operators for test

-r filenm True if the user has read permission

-w filenm True if the user has write permission

-x filenm True if the user has execute permission

-f filenm True if filenm is a regular file

-d filenm True if filenm is a directory


File operators for test

-c filenm True if filenm is a character special file

-b filenm True if filenm is a block special file

-s filenm True if the size of filenm is not zero


Combining Conditions

The primary expressions in a test


command can be combined with
a logical and operator, -a,
a logical or operator, -o ,
a logical not operator, !
A notational shorthand

Bourne shell has an alternative


method for using test: you enclose
the entire expression in square
brackets ([]) You don’t have to write
‘test’.
Reading Data into a
Program
read var1 var2 ... varn
$ read var1 var2 var3
Hello my dear friend
$ echo $var1
Hello
$ echo $var2
my
$ echo $var3
dear friend
Reading Data into a
Program
$ read var1 var2 var3
Hello friend
$ echo $var1 Hello
$ echo $var2
friend
$ echo $var3
$
The case Statement
case value in
pattern1) command;;
pattern2) command;;
...
patternn) command;
Esac
Note
The case statement executes only one set of
commands. If the value matches more than one
of the patterns, only the first set of commands
specified is executed. The double semicolons (;;)
after a command act as the delimiter of the
commands to be executed for a particular pattern
match.
The Case Statement
Multiple patterns to be related to the same
set of commands. Use a vertical bar (|)
read CHOICE
case "$CHOICE" in
1 | R )…;;
2 | B ) …;;
*) echo "Sorry, $CHOICE is not a valid
choice"
exit 1
esac
Repeating Within a while
and until
while command
do
command
done
until command
do
command
done
Repeating with for
 for variable in arg1 arg2 ... argn
do
command
done
Loop Handling

break command causes the program


to exit the loop immediately
continue command causes the
program to skip the remaining
commands in the loop but remain in
the loop

You might also like