Shell脚本结构流程控制
2014-03-03 09:31
316 查看
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的
相关文章推荐
- [笔记]shell脚本中的流程控制
- shell脚本之流程控制语句
- Linux的Shell脚本中IF流程控制语句的基本语法
- Shell脚本学习笔记(四)--流程控制
- shell脚本流程控制
- 几类脚本的流程控制语句(shell,python,lua)
- shell脚本编程之选择控制结构 推荐
- shell脚本之流程控制
- Shell脚本学习笔记(四)--流程控制
- shell脚本流程控制
- Shell脚本的基本流程控制
- Shell脚本(2)之条件测试和流程控制
- NO4.Shell脚本学习——Shell条件测试和流程控制
- SHELL脚本中常用的控制结构
- 【Shell 编程基础第二部分】Shell里的流程控制\函数及\脚本调试
- 3、shellwhile循环,C-for循环,until循环,case分支结构,流程控制语句
- Shell里的流程控制、Shell里的函数及脚本调试
- shell脚本编程之循环控制结构
- 【Shell 编程基础第二部分】Shell里的流程控制、Shell里的函数及脚本调试方法
- shell脚本编程——流程控制