if condition
In shell script if condition is used in making decision , The command or given statement is executed if the condition is true.
Syntax:
if condition
then
command if condition is true or if exit status
of condition is 0 (zero)
...
...
fi
Condition can be defined as: "The if Condition is nothing but it is comparison between given two values."
For comparison we can use test or [ expr ] statements or even exist status can be also used in if condition.
Expression can be defined as: "An expression is the combination of values, relational operators like >,<, <> etc. and mathematical operators like +, -, / etc."
Given below are all example of expressions:
4 > 3
6 + 4
4 * 56
x < y
z > 7
z > 3 + 45 -23
Type following commands
$ ls
$ echo $?
The cat command returned the value zero(0) i.e. exit status, on successful execution of command, this can be used in if condition as follows, Write shell script as
$ cat > test.sh
if cat $1
then
echo -e "nnFile $1, found and successfully displayed the contents."
fi
Run above script as:
$ sh test.sh
Shell script name is test.sh ($0) and sunni is argument (which is $1).Then shell compare it as follows:
if cat $1 which is expanded to if cat sunni.
In shell script if condition is used in making decision , The command or given statement is executed if the condition is true.
Syntax:
if condition
then
command if condition is true or if exit status
of condition is 0 (zero)
...
...
fi
Condition can be defined as: "The if Condition is nothing but it is comparison between given two values."
For comparison we can use test or [ expr ] statements or even exist status can be also used in if condition.
Expression can be defined as: "An expression is the combination of values, relational operators like >,<, <> etc. and mathematical operators like +, -, / etc."
Given below are all example of expressions:
4 > 3
6 + 4
4 * 56
x < y
z > 7
z > 3 + 45 -23
Type following commands
$ ls
$ echo $?
The cat command returned the value zero(0) i.e. exit status, on successful execution of command, this can be used in if condition as follows, Write shell script as
$ cat > test.sh
if cat $1
then
echo -e "nnFile $1, found and successfully displayed the contents."
fi
Run above script as:
$ sh test.sh
Shell script name is test.sh ($0) and sunni is argument (which is $1).Then shell compare it as follows:
if cat $1 which is expanded to if cat sunni.
0 Comments:
Post a Comment