- if 条件语句:
if condition which is used for decision making in shell script, If given condition is true then command1 is executed.
Syntax:
if condition
then
command1 if condition is true or if exit status
of condition is 0 (zero)
...
...
fi
Condition is defined as:
"Condition is nothing but comparison between two values. "
哈哈,很喜欢这个词组: ... is nothing but ...
很有藐视一切的气概!
希望等我看着这本书,我也可以说句 shell is nothing but combination of some linux command!
if...else...fi
If given condition is true then command1 is executed otherwise command2 is executed.
Syntax:
if condition
then
condition is zero (true - 0)
execute all commands up to else statement
else
if condition is not true then
execute all commands up to fi
fi
嵌套的条件语句:
You can use the nested if s as follows also:
Syntax:
if condition
then
if condition
then
.....
..
do this
else
....
..
do this
fi
else
...
.....
do this
fi
if 后跟多个else语句:
if condition
then
condition is zero (true - 0)
execute all commands up to elif statement
elif condition1
then
condition1 is zero (true - 0)
execute all commands up to elif statement
elif condition2
then
condition2 is zero (true - 0)
execute all commands up to elif statement
else
None of the above condtion,condtion1,condtion2 are true (i.e.
all of the above nonzero or false)
execute all commands up to fi
fi
2 test commnad or [expr]
test command or [ expr ] is used to see if an expression is true, and if
it is true it return zero(0), otherwise returns nonzero for false.
Syntax:
test expression OR [ expression ]
test or [ expr ] works with
1.Integer ( Number without decimal point)
2.File types
3.Character strings
For Mathematics, use following operator in Shell Script
Mathematical Operator in Shell Script | Meaning | Normal Arithmetical/ Mathematical Statements | But in Shell | |
For test statement with if command | For [ expr ] statement with if command | |||
-eq | is equal to | 5 == 6 | if test 5 -eq 6 | if [ 5 -eq 6 ] |
-ne | is not equal to | 5 != 6 | if test 5 -ne 6 | if [ 5 -ne 6 ] |
-lt | is less than | 5 < 6 | if test 5 -lt 6 | if [ 5 -lt 6 ] |
-le | is less than or equal to | 5 <= 6 | if test 5 -le 6 | if [ 5 -le 6 ] |
-gt | is greater than | 5 > 6 | if test 5 -gt 6 | if [ 5 -gt 6 ] |
-ge | is greater than or equal to | 5 >= 6 | if test 5 -ge 6 | if [ 5 -ge 6 ] |
NOTE: == is equal, != is not equal.
For string Comparisons use
Operator | Meaning |
string1 = string2 | string1 is equal to string2 |
string1 != string2 | string1 is NOT equal to string2 |
string1 | string1 is NOT NULL or not defined |
-n string1 | string1 is NOT NULL and does exist |
-z string1 | string1 is NULL and does exist |
Shell also test for file and directory types
Test | Meaning |
-s file | Non empty file |
-f file | Is File exist or normal file and not a directory |
-d dir | Is Directory exist and not a file |
-w file | Is writeable file |
-r file | Is read-only file |
-x file | Is file is executable |
Logical Operators
Logical operators are used to combine two or more condition at a time
Operator | Meaning |
! expression | Logical NOT |
expression1 -a expression2 | Logical AND |
expression1 -o expression2 | Logical OR |
3 for 循环:
for { variable name } in { list }
do
execute one for each item in the list until the list is
not finished (And repeat all statement between do and done)
done
第二种for 循环
Syntax:
for (( expr1; expr2; expr3 ))
do
..... ... repeat all statements between do and done until expr2 is TRUE Done
4 while 循环:
while [ condition ]5 case 条件语句:
do
command1
command2
command3
..
....
done
Syntax:
cas e $variable-name in
pattern1) command
...
..
command;;
pattern2) command
...
..
command;;
patternN) command
...
..
command;;
*) command
...
..
command;;
esac
几点注意: patten 后面)不能丢掉
以上这些内容都比较基础,看完就差不多会用,所以没什么好讲的。例子也不举了