Linux
Linux
Class : II B.Sc. CS
Semester : IV
Prepared by : Dr.R.Aswanandini
Assistant Professor
Department of Computer Science
KG College of Arts and Science
Unit - V
CONDITIONAL EXECUTION IN
SHELL SCRIPTS
UNIT V
• if [ expression ]
• then
• Statement(s) to be executed if expression is true
• fi
• #!/bin/sh
• a=10
• b=20
• if [ $a == $b ]
• then
• echo "a is equal to b“
• fi
• if [ $a != $b ]
• then
• echo "a is not equal to b“
• fi
if...else...fi :
• The if...else...fi statement is the next form of control statement that
allows Shell to execute statements in a controlled way and make the
right choice.
• if [ expression ]
• then
• Statement(s) to be executed if expression is true
• else
• Statement(s) to be executed if expression is not true
• fi
• #!/bin/sh
• a=10
• b=20
• if [ $a == $b ]
• then
• echo "a is equal to b“
• else
• echo "a is not equal to b“
• fi
if...elif...else...fi statement :
• The if...elif...fi statement is the one level advance form of control
statement that allows Shell to make correct decision out of several
conditions.
• if [ expression 1 ]
• then
• Statement(s) to be executed if expression 1 is true
• elif [ expression 2 ]
• then
• Statement(s) to be executed if expression 2 is
• true
• elif [ expression 3 ]
• then
• Statement(s) to be executed if expression 3 is true
• else
• Statement(s) to be executed if no expression is true
• fi
• #!/bin/sh
• a=10
• b=20
• if [ $a == $b ]
• then
• echo "a is equal to b“
• elif [ $a -gt $b ]
• then
• echo "a is greater than b“
• elif [ $a -lt $b ]
• then
• echo "a is less than b“
• else
• echo "None of the condition met“
• fi
Unit - V
CONDITIONAL EXECUTION IN
SHELL SCRIPTS
Control Statements
#!/bin/sh
# timestable – print out a multiplication table
for i in 1 2 3
do
for j in 1 2 3
do
value=`expr $i \* $j`
echo -n "$value "
done
echo
done
for Loop
#!/bin/sh Example (2)
# file-poke – tell us stuff about files
files=`ls`
for i in $files
do
echo -n "$i "
grep $i $i
done
• Example:
% read X Y Z
Here are some words as input
% echo $X
Here
% echo $Y
are
% echo $Z
some words as input
The case Statement
• The case statement supports multiway branching based on the value of a single string.
• General form:
case string in
pattern1)
command_set_11
;;
pattern2)
command_set_2
;;
…
esac
case Example
#!/bin/sh
read reply
Use the pipe symbol “|” as a logical
echo
or between several choices.
case $reply in
"1")
date
;;
"2"|"3")
pwd
;;
"4")
ls
;;
Provide a default case when no
*)
other cases are matched.
echo Illegal choice!
;;
esac
Unit - V
CONDITIONAL EXECUTION IN
SHELL SCRIPTS
Control Statements
#!/bin/sh
# timestable – print out a multiplication table
for i in 1 2 3
do
for j in 1 2 3
do
value=`expr $i \* $j`
echo -n "$value "
done
echo
done
for Loop
#!/bin/sh Example (2)
# file-poke – tell us stuff about files
files=`ls`
for i in $files
do
echo -n "$i "
grep $i $i
done
• Example:
% read X Y Z
Here are some words as input
% echo $X
Here
% echo $Y
are
% echo $Z
some words as input
The case Statement
• The case statement supports multiway branching based on the value of a single string.
• General form:
case string in
pattern1)
command_set_11
;;
pattern2)
command_set_2
;;
…
esac
case Example
#!/bin/sh
read reply
Use the pipe symbol “|” as a logical
echo
or between several choices.
case $reply in
"1")
date
;;
"2"|"3")
pwd
;;
"4")
ls
;;
Provide a default case when no
*)
other cases are matched.
echo Illegal choice!
;;
esac
Unit - V
The while Loop
&The until Loop
The while Loop
#!/bin/sh
x=1
until [ $x -gt 3 ]; do
echo x = $x
x=`expr $x + 1`
done
Command Line Arguments (1)
• Special variables:
• $*, $# -- all the arguments, the number of
the arguments
• $$ -- the process id of the current shell
• $? -- return value of last foreground
process to finish
-- more on this one later
• There are others you can find out about with man sh
Unit - V
The break and continue
Statements
The break and continue Statements
• break
– Causes immediate exit from a while, for, do/while or
–switch
Programstructure
execution continues with the first statement after the structure
– Common uses of the break statement
• Escape early from a loop
• Skip the remainder of a switch structure
The break and continue Statements (II)
• continue
– Skips the remaining statements in the body of a while, for or
do/while structure
• Proceeds with the next iteration of the loop
– while and do/while
• Loop-continuation test is evaluated immediately after the
continue statement is executed
– for structure
• Increment expression is executed, then the loop-continuation test is evaluated
1 /* Fig. 4.12: fig04_12.c
2 Using the continue statement in a for Outline
3 #include*/<stdio.h>
structure
4
5 int main() 6 { 1. Initialize variable
7 int x; 8
9
2. Loop
10
11 for ( x = 1; x <= 10; x++ ) {
12 3. Print
13 if ( x == 5 )
14 continue; /* skip remaining code in if x
15
loop only == 5 */
16 }
17 printf( "%d ", x );
18 printf( "\nUsed continue to skip printing
19 return 0;
1 2 3 4 6 7 8 9 10 Program Output
Used continue to skip printing the value 5
true
Statement(s)
break
Statement(s)
Next
Statement
break
// Using break to exit a loop (break.c). #include
<stdio.h>
main() {
int i;
for(i=0; i<100; i++) {
if(i == 10) break; // terminate loop if i is 10 printf("i: %d
\n", i);
}
printf("Loop complete.");
}
The continue Keyword false
Continue
condition?
true
Statement(s)
continue
Statement(s)
Next
Statement