CONTROL STATEMENTS
IN
SHELL PROGRAMMING
BHATVIJETHA
CONTROL STATEMENTS
If
If(conditional_command) format1
then
<commands>
fi
If(conditional_command) format2
then
<commands>
else
<commands>
fi
if (conditional_command) format3
then
<commands>
elif(conditional_command)
then
<commands>
..
else
<commands>
fi
• If the specified <conditional_command> is
executed successfully then the commands in
between then and else will be executed.
• Otherwise if the specified
<conditional_command>is not executed
successfully then the commands in between else
and fi will be executed.
Test command
• This command is used to test the validity of its arguments . And mostly this
command is used in conditional _commands.
• This test command returns an exit status 0 if test succeeds and 1 false if the test fails.
General format is,
Test expression
String comparision :
general format is , test operation
Where the operation can be,
String 1=string2 - compares two strings ; returns true if both
are equal else return false
String1!=string2 - compares two strings; strings ; returns true
if both are equal else return false
-z string - checks whether the specified string is of zero or
not, returns true if the string is null, else return
false
-n string - checks whether the specified string is of zero
or not,returns true if the string is null,else return
false
Numerical comparision
operations meaning
-eq Returns true if number1 is equal to number2..else false.
-ne Returns true if number1 is not equal to number2 else returns false.
-gt Returns true if number1 is greater than number2 else returns false.
-lt Returns true if number1 is less than number2 else returns false.
-ge Returns true if number1 is less than number2 else returns false.
-le Returns true if number1 is less than or equal to number2 else
returns false.
General format is,
Test number1 operator number2
Case statement
This is a multi- branch control statement.
General format is,
Case value is,
Pattern1) <commands>;;
Pattern2)<commands>;;
..
patternN)<commands>;;
Esac
• This statement compares the value to the patterns from
the top to bottom , and performs the commands
associated with the matching pattern.
• The commands for each pattern must be terminated by
double semicolon.
End of presentation

LINUX:Control statements in shell programming

  • 1.
  • 2.
  • 3.
  • 4.
    • If thespecified <conditional_command> is executed successfully then the commands in between then and else will be executed. • Otherwise if the specified <conditional_command>is not executed successfully then the commands in between else and fi will be executed.
  • 5.
    Test command • Thiscommand is used to test the validity of its arguments . And mostly this command is used in conditional _commands. • This test command returns an exit status 0 if test succeeds and 1 false if the test fails. General format is, Test expression String comparision : general format is , test operation Where the operation can be, String 1=string2 - compares two strings ; returns true if both are equal else return false String1!=string2 - compares two strings; strings ; returns true if both are equal else return false -z string - checks whether the specified string is of zero or not, returns true if the string is null, else return false -n string - checks whether the specified string is of zero or not,returns true if the string is null,else return false
  • 6.
    Numerical comparision operations meaning -eqReturns true if number1 is equal to number2..else false. -ne Returns true if number1 is not equal to number2 else returns false. -gt Returns true if number1 is greater than number2 else returns false. -lt Returns true if number1 is less than number2 else returns false. -ge Returns true if number1 is less than number2 else returns false. -le Returns true if number1 is less than or equal to number2 else returns false. General format is, Test number1 operator number2
  • 7.
    Case statement This isa multi- branch control statement. General format is, Case value is, Pattern1) <commands>;; Pattern2)<commands>;; .. patternN)<commands>;; Esac
  • 8.
    • This statementcompares the value to the patterns from the top to bottom , and performs the commands associated with the matching pattern. • The commands for each pattern must be terminated by double semicolon.
  • 9.