Shell中结构语法

  1. 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
-eqis equal to5 == 6if test 5 -eq 6if [ 5 -eq 6 ]
-ne is not equal to5 != 6if test 5 -ne 6if [ 5 -ne 6 ]
-lt is less than5 < 6if test 5 -lt 6if [ 5 -lt 6 ]
-le is less than or equal to5 <= 6if test 5 -le 6if [ 5 -le 6 ]
-gt is greater than5 > 6if test 5 -gt 6if [ 5 -gt 6 ]
-ge is greater than or equal to5 >= 6if test 5 -ge 6if [ 5 -ge 6 ]

NOTE: == is equal, != is not equal.

For string Comparisons use

Operator Meaning
string1 = string2string1 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 ]

do
command1
command2
command3
..
....

done

5 case 条件语句:

Syntax:
           cas
e  $variable-name  in

pattern1) command
...
..
command;;
pattern2) command
...
..
command;;
patternN) command
...
..
command;;
*) command
...
..
command;;

esac
几点注意: patten 后面)不能丢掉

以上这些内容都比较基础,看完就差不多会用,所以没什么好讲的。例子也不举了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

惹不起的程咬金

来都来了,不赏点银子么

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值