Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
52 views

Shell Programmingifelse String File Part4

The document discusses the if-else conditional statement in shell scripting. It provides the syntax of if-else and examples to check various conditions using relational, boolean, string and file test operators. The key aspects covered are: - The if-else statement syntax and basic usage to check for true or false conditions - Examples using relational operators like -eq, -ne, -gt to check equality, inequality and other relations - Boolean operators like -a and -o to check logical AND and OR conditions - String operators like =, != to check for string equality and inequality - File test operators like -r, -w, -x to check permissions and -f, -d to check

Uploaded by

Pragat Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Shell Programmingifelse String File Part4

The document discusses the if-else conditional statement in shell scripting. It provides the syntax of if-else and examples to check various conditions using relational, boolean, string and file test operators. The key aspects covered are: - The if-else statement syntax and basic usage to check for true or false conditions - Examples using relational operators like -eq, -ne, -gt to check equality, inequality and other relations - Boolean operators like -a and -o to check logical AND and OR conditions - String operators like =, != to check for string equality and inequality - File test operators like -r, -w, -x to check permissions and -f, -d to check

Uploaded by

Pragat Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

SHELL PROGRAMMING PART 4

Unix / Linux Shell - The if...else...fi statement

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.

Syntax
if [ expression ]
then
Statement(s) to be executed if expression is true
else
Statement(s) to be executed if expression is not true
fi

The Shell expression is evaluated in the above syntax. If the resulting value is true, given statement(s) are
executed. If the expression is false, then no statement will be executed.

Example
The above example can also be written using the if...else statement as follows −
Live Demo

#!/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

Upon execution, you will receive the following result −


a is not equal to b
Relational Operators

Operator Description Example

-eq Checks if the value of two operands are [ $a -eq $b ] is not true.
equal or not; if yes, then the condition
becomes true.

-ne Checks if the value of two operands are


equal or not; if values are not equal, then [ $a -ne $b ] is true.
the condition becomes true.

-gt Checks if the value of left operand is


greater than the value of right operand; if [ $a -gt $b ] is not true.
yes, then the condition becomes true.

-lt Checks if the value of left operand is less


than the value of right operand; if yes, then [ $a -lt $b ] is true.
the condition becomes true.

-ge Checks if the value of left operand is


greater than or equal to the value of right
[ $a -ge $b ] is not true.
operand; if yes, then the condition becomes
true.
-le Checks if the value of left operand is less
than or equal to the value of right operand; [ $a -le $b ] is true.
if yes, then the condition becomes true.

It is very important to understand that all the conditional expressions should be placed inside square braces with
spaces around them. For example, [ $a <= $b ] is correct whereas, [$a <= $b] is incorrect.

Example
Here is an example which uses all the relational operators −
Live Demo

#!/bin/sh

a=10

b=20

if [ $a -eq $b ]

then

echo "$a -eq $b : a is equal to b"

else

echo "$a -eq $b: a is not equal to b"


fi

if [ $a -ne $b ]

then

echo "$a -ne $b: a is not equal to b"

else

echo "$a -ne $b : a is equal to b"

fi

if [ $a -gt $b ]

then

echo "$a -gt $b: a is greater than b"

else

echo "$a -gt $b: a is not greater than b"

fi

if [ $a -lt $b ]

then

echo "$a -lt $b: a is less than b"


else

echo "$a -lt $b: a is not less than b"

fi

if [ $a -ge $b ]

then

echo "$a -ge $b: a is greater or equal to b"

else

echo "$a -ge $b: a is not greater or equal to b"

fi

if [ $a -le $b ]

then

echo "$a -le $b: a is less or equal to b"

else

echo "$a -le $b: a is not less or equal to b"

fi
Boolean Operators
The following Boolean operators are supported by the Bourne Shell.

Assume variable a holds 10 and variable b holds 20 then −

Show Examples

Operator Description Example

! This is logical negation. This


inverts a true condition into false [ ! false ] is true.
and vice versa.

-o This is logical OR. If one of the [ $a -lt 20 -o $b -gt 100 ] is true.


operands is true, then the
condition becomes true.

-a This is logical AND. If both the


operands are true, then the
[ $a -lt 20 -a $b -gt 100 ] is false.
condition becomes true otherwise
false.
#!/bin/sh

a=10

b=20

if [ $a != $b ]

then

echo "$a != $b : a is not equal to b"

else

echo "$a != $b: a is equal to b"

fi

if [ $a -lt 100 -a $b -gt 15 ]

then

echo "$a -lt 100 -a $b -gt 15 : returns true"

else

echo "$a -lt 100 -a $b -gt 15 : returns false"

fi
if [ $a -lt 100 -o $b -gt 100 ]

then

echo "$a -lt 100 -o $b -gt 100 : returns true"

else

echo "$a -lt 100 -o $b -gt 100 : returns false"

fi

if [ $a -lt 5 -o $b -gt 100 ]

then

echo "$a -lt 100 -o $b -gt 100 : returns true"

else

echo "$a -lt 100 -o $b -gt 100 : returns false"

fi
String Operators
The following string operators are supported by Bourne Shell.

Assume variable a holds "abc" and variable b holds "efg" then −

Show Examples

Operator Description Example

= Checks if the value of two operands are [ $a = $b ] is not true.


equal or not; if yes, then the condition
becomes true.

!= Checks if the value of two operands are [ $a != $b ] is true.


equal or not; if values are not equal then the
condition becomes true.

-z Checks if the given string operand size is [ -z $a ] is not true.


zero; if it is zero length, then it returns true.

-n Checks if the given string operand size is [ -n $a ] is not false.


non-zero; if it is nonzero length, then it
returns true.
str Checks if str is not the empty string; if it is [ $a ] is not false.
empty, then it returns false.

#!/bin/sh

a="abc"

b="efg"

if [ $a = $b ]

then

echo "$a = $b : a is equal to b"

else

echo "$a = $b: a is not equal to b"

fi

if [ $a != $b ]

then

echo "$a != $b : a is not equal to b"

else
echo "$a != $b: a is equal to b"

fi

if [ -z $a ]

then

echo "-z $a : string length is zero"

else

echo "-z $a : string length is not zero"

fi

if [ -n $a ]

then

echo "-n $a : string length is not zero"

else

echo "-n $a : string length is zero"

fi

if [ $a ]

then
echo "$a : string is not empty"

else

echo "$a : string is empty"

fi

File Test Operators


We have a few operators that can be used to test various properties associated with a Unix file.

Assume a variable file holds an existing file name "test" the size of which is 100 bytes and
has read, write and execute permission on −

We have a few operators that can be used to test various properties associated with a Unix file.

Assume a variable file holds an existing file name "test" the size of which is 100 bytes and
has read, write and execute permission on −

Operator Description Example

-b file Checks if file is a block special file; if yes, [ -b $file ] is false.


then the condition becomes true.
-c file Checks if file is a character special file; if yes, [ -c $file ] is false.
then the condition becomes true.

-d file Checks if file is a directory; if yes, then the [ -d $file ] is not true.
condition becomes true.

-f file Checks if file is an ordinary file as opposed to [ -f $file ] is true.


a directory or special file; if yes, then the
condition becomes true.

-g file Checks if file has its set group ID (SGID) bit [ -g $file ] is false.
set; if yes, then the condition becomes true.

-k file Checks if file has its sticky bit set; if yes, then [ -k $file ] is false.
the condition becomes true.

-p file Checks if file is a named pipe; if yes, then the [ -p $file ] is false.
condition becomes true.

-t file Checks if file descriptor is open and [ -t $file ] is false.


associated with a terminal; if yes, then the
condition becomes true.
-u file Checks if file has its Set User ID (SUID) bit [ -u $file ] is false.
set; if yes, then the condition becomes true.

-r file Checks if file is readable; if yes, then the [ -r $file ] is true.


condition becomes true.

-w file Checks if file is writable; if yes, then the [ -w $file ] is true.


condition becomes true.

-x file Checks if file is executable; if yes, then the [ -x $file ] is true.


condition becomes true.

-s file Checks if file has size greater than 0; if yes, [ -s $file ] is true.
then condition becomes true.

-e file Checks if file exists; is true even if file is a [ -e $file ] is true.


directory but exists.
Example
The following example uses all the file test operators −

Assume a variable file holds an existing file name "/var/www/tutorialspoint/unix/test.sh" the size of which
is 100 bytes and has read, write and execute permission −
Live Demo

#!/bin/sh

file="/var/www/tutorialspoint/unix/test.sh"

if [ -r $file ]

then

echo "File has read access"

els e

echo "File does not have read access"

fi

if [ -w $file ]
then

echo "File has write permission"

else

echo "File does not have write permission"

fi

if [ -x $file ]

then

echo "File has execute permission"

else

echo "File does not have execute permission"

fi

if [ -f $file ]

then

echo "File is an ordinary file"

else

echo "This is sepcial file"

fi
if [ -d $file ]

then

echo "File is a directory"

else

echo "This is not a directory"

fi

if [ -s $file ]

then

echo "File size is zero"

else

echo "File size is not zero"

fi

if [ -e $file ]

then

echo "File exists"

else
echo "File does not exist"

fi

You might also like