1. 分支与判定
if分支
if then fi
if then else fi
if then elif then else fi
case分支
case语句
case test-string in
pattern1) operation1
;;
pattern2) operation2
;;
pattern3) operation3
;;
pattern4) operation4
;;
*) operation5 ;; #接受所有的参数,相当于default
esac
另外,对于patter还可以是 pattern1 | pattern1' ), 例如 a | A )表示接受大小写的a
test 判定
test [expression] 或 [[expression]], test可以测试很多内容,并且把结果作为整个表达式的值返回去。要注意的还有,就是说test中,任何一个运算符、圆括号、方括号或操作数的前后至少需要一个空格。如果要延续到下一行,则使用\
test可以作的文件测试
-d file file为目录的时候,为真
-f file file为文件的时候,true
-r file 可读的时候为true
-s file 长度大于0,为true
-t [filedes]文件描述符与终端相连的时候为真
-w file 可写的时候为真
-x file 可执行的时候为真
test可以作的整数测试
int1 -eq int2 相等
int1 -ge int2 大于等于
int1 -gt int2 大于
int1 -le int2 小于等于
int1 -lt int2 小于
int1 -ne int2 不等
test可以作的字符串测试
str 非空串为真
str1 = str2 相同的时候为真
str1 != str2 不同的时候为真
-n str 长度大于0的时候为真
-z str 长度等于0的时候为真
2. 循环结构
for循环
for variable [in arg-list]
do
operation
done
执行的次数会跟arg-list中的参数数量一样多,每次把参数列表中的一个值赋值给变量,然后作循环
也可以直接通过shell给脚本参数变量
while循环
while expression
do
operation
done
until循环
until expression
do
operation
done
break和continue同c和java的
if分支
if then fi
if then else fi
if then elif then else fi
case分支
case语句
case test-string in
pattern1) operation1
;;
pattern2) operation2
;;
pattern3) operation3
;;
pattern4) operation4
;;
*) operation5 ;; #接受所有的参数,相当于default
esac
另外,对于patter还可以是 pattern1 | pattern1' ), 例如 a | A )表示接受大小写的a
test 判定
test [expression] 或 [[expression]], test可以测试很多内容,并且把结果作为整个表达式的值返回去。要注意的还有,就是说test中,任何一个运算符、圆括号、方括号或操作数的前后至少需要一个空格。如果要延续到下一行,则使用\
test可以作的文件测试
-d file file为目录的时候,为真
-f file file为文件的时候,true
-r file 可读的时候为true
-s file 长度大于0,为true
-t [filedes]文件描述符与终端相连的时候为真
-w file 可写的时候为真
-x file 可执行的时候为真
test可以作的整数测试
int1 -eq int2 相等
int1 -ge int2 大于等于
int1 -gt int2 大于
int1 -le int2 小于等于
int1 -lt int2 小于
int1 -ne int2 不等
test可以作的字符串测试
str 非空串为真
str1 = str2 相同的时候为真
str1 != str2 不同的时候为真
-n str 长度大于0的时候为真
-z str 长度等于0的时候为真
2. 循环结构
for循环
for variable [in arg-list]
do
operation
done
执行的次数会跟arg-list中的参数数量一样多,每次把参数列表中的一个值赋值给变量,然后作循环
也可以直接通过shell给脚本参数变量
while循环
while expression
do
operation
done
until循环
until expression
do
operation
done
break和continue同c和java的