shell 参数传递、判断、循环
来源:互联网 发布:ubuntu 12.04更新内核 编辑:程序博客网 时间:2024/04/29 04:12
http://fightfxj.blog.163.com/blog/static/67683924200921111590990/
1,参数传递
执行程序时,自动把参数按顺序存放在$1,$2,...中,超过10个参数,用${n}表示
$*引用所有参数
$#存放参数的个数
$0引用程序名
shift命令:
作用是把位置参数左移,原来$2中的参数到$1中,以此类推,$#也自动减一
2,判断
(1)$?变量
自动将最后执行命令的退出状态存放到$?中,退出状态为0表示正常退出
(2)/dev/null
系统的特殊文件,任何人都可以向其中读取和写入,当向它写入时,写入的数据马上被丢弃
(3)test命令
test expression
expression表示要测试的条件,如果为真,退出状态为0
另一种形式[expression]
字符串操作符:
String1=String2 字符串相同
String1!=String2 字符串不相同
String 字符串不为空
-n String 字符串不为空,并且test能够看到string
-z String 字符串为空,并且test能够看到string
整数操作符:
int1 -eq int2
int1 -ge int2
int1 -gt int2
int1 -le int2
int1 -lt int2
int1 -ne int2
例如:
["$count" -eq 0]
文件操作符:
-d file:为目录
-e file:存在
-f file:为普通文件
-r file:为进程可读文件
-s file:长度不为0
-w file:为进程可写文件
-x file:可执行
-L file:为符号链接
逻辑操作符:
!:非
-a:与
-o:或
例如:
[! -r /users/more/test]
(4)if else结构
if command1
then
command
command
...
else
command
command
fi
(5)exit命令
exit n
n为希望返回的退出状态
(6)elif结构
if command
then
command
command
...
elif command
then
command
command
elif command
then
command
command
...
fi
(7)case命令
case value in
pat1) command #条件形式可以采用正则表达式
...
command;; #执行到双分号,该case语句就结束了,不会继续判断后面的条件
pat2) command
...
command;;
...
esac
例如:
case "$1" in
0) echo zero;;
1) echo one;;
...
esac
(8)&&和||结构
command1 && command2,如果command1退出状态为0,则执行command2
command1 || command2,如果command1退出状态不为0,则执行command2
3,循环
(1)for命令
for var in word1 word2 ... wordn
do
command
command
...
done
例如:
for i in 1 2 3
do
echo $i
done
(2)$@变量
如果没有双引号$@与$*完全一样,表示所有的参数,有双引号,就保留参数的实际样子
例如:
$cat args
number of arguments passed is $#
for arg in "$@"
do
echo $arg
done
$args 'a b' c
运行结果:
number of arguments passed is 2
a b #程序中如果写成for arg in $*, 则会将a,b作为两个不同的循环变量,虽然参数的数目仍然是2
c
(3)不带列表的for循环
for var
do
command
command
...
done
会自动将参数依次组织成列表
例如上面的args程序:
number of arguments passed is $#
for arg
do
echo $arg
done
(4)while命令
while command1 #command1的退出状态为0,就一直执行
do
command
command
...
done
(5)until命令
until command1 #command1的退出状态不为0,就一直执行
do
command
command
...
done
(6)其他指令
break:中断循环
continue:跳过循环中的后续命令
done &:在后台执行循环
done > loopout:循环输出重定向到loopout
done | wc -l:通过管道输入数据或接受循环的输出数据
单行循环
for i in 1 2 3 4; do echo $i; done
对while和until也类似,if也是。用分号隔开语句。
- shell 参数传递、判断、循环
- 循环addEventListener传递参数
- shell 变量 判断 循环
- shell 循环判断语法
- shell运算、判断、循环
- shell--赋值,判断,循环
- shell参数传递
- shell参数传递
- shell 交互式参数传递
- shell编程参数传递
- linux shell 参数传递
- shell 函数参数传递
- linux shell参数传递
- sqlplus传递参数shell
- Shell 传递参数
- shell--5--传递参数
- Shell 传递参数
- shell传递参数
- tomcat调优
- MyEclipse中部署SVN的几种方法收集
- android popupwindow中edittext获取焦点问题
- android UI组件大全
- 根据规则来判别不同的数据!
- shell 参数传递、判断、循环
- 非官方的iOS设计指南
- android 隐藏系统键盘
- wamp5 Apache启动失败的解决方法
- VS各种文件作用详解
- spring自学的深度笔记--博客汇总
- PKI 系统介绍
- HttpClient的CircularRedirectException异常原因及解决办法
- cocoaPods 的安装和使用