Shell中判断各种比较
来源:互联网 发布:安卓系统刷windows系统 编辑:程序博客网 时间:2024/05/22 04:34
1.
-f
-d
-b
-c
-S
-L
-e
2.
-G
-O
-p
3.
-r
-w
-x
-s
-u
-g
-k
4.
-nt 第一个档案比第二个档案新
-ot 第一个档案比第二个档案旧
-ef 第一个档案与第二个档案为同一个档案( link 之类的档案)
5.
&&
||
运算符号
=
!=
<
-eq 等于 应用于:整型比较
-ne 不等于 应用于:整型比较
-lt 小于 应用于:整型比较
-gt 大于 应用于:整型比较
-le 小于或等于 应用于:整型比较
-ge 大于或等于 应用于:整型比较
-a
-o
-z
-n
所以你的循环条件部分写成这样才是对的:
while [ "${conunter}" -lt 5 ]; do
注意在条件检测中把变量名引起来的双引号不能省略,这个地方如果将来是字符串的判定,没有双引号在某些条件下会被玩死的。所以最好养成习惯,无论是不是数值都把变量名用双引号引起来。另外,做累加操作还有别的方法,像下面这样:
#!/bin/ksh
counter=0
while [ "${counter}" -lt 5 ]; do
((counter++))
echo "counter is $counter"
done
for i in $(seq 0 99); do [ $i lt 10 ] && useradd student0$i || useradd student$i; done
shell中的特殊变量:
变量名
含义
$0
shell或shell脚本的名字
$*
以一对双引号给出参数列表
$@
将各个参数分别加双引号返回
$#
参数的个数
$_
代表上一个命令的最后一个参数
$!
代表最后执行的后台命令的PID
$?
代表上一个命令执行后的退出状态
e.g.
编辑如下test.sh脚本
#!/bin/bash
echo $0
echo $*
echo $@
echo $#
echo
ls -a /home
echo $_
在terminal窗口中执行:
xk@linux:~/work> ./test.sh -a -b -c /home
./test.sh
-a -b -c /home
-a -b -c /home
4
3250
. .. fy jodier sky xk zhj
/home
xk@linux:~/work>echo $?
0
xk@linux:~/work>echo $!
xk@linux:~/work> ls -a /home &
[1] 3302
xk@linux:~/work> . .. fy jodier sky xk zhj
[1]+ Done /bin/ls $LS_OPTIONS -a /home
xk@linux:~/work> echo $!
3302
xk@linux:~/work>
为了区别$*和$@编写如下test.sh脚本:
#!/bin/bash
function testargs
{
echo "$# args"
}
testargs "$*"
testargs "$@"
unset -f testargs
在terminal窗口中执行:
xk@linux:~/work> ./test.sh -a -b /home
1 args
3 args
xk@linux:~/work>
这里有一个很有意思的问题,如果test.sh为如下的内容:
#!/bin/bash
function testargs
{
echo "$# args"
}
testargs $*
testargs $@
unset -f testargs
再次执行有:
xk@linux:~/work> ./test.sh -a -b /home
3 args
3 args
- Shell中判断各种比较
- shell中各种比较关系
- shell中的各种判断
- shell各种判断手册
- shell 比较判断
- shell判断和比较
- shell判断和比较
- shell判断和比较
- shell判断和比较
- shell判断和比较
- shell判断和比较
- shell判断和比较
- Shell判断和比较
- Shell判断和比较
- Shell中条件判断if中的各种参数的意思
- shell脚本的各种判断
- shell脚本的各种判断
- shell脚本的各种判断
- Visual Studio中Debug与Release编译模式的区别以及Preprocesor directives的使用
- MYSQL 索引及优化
- 《UNIX网络编程》TCP网络编程基础(1)
- 使图片垂直&水平居中的CSS实现方法
- MyEclipse不编译问题的解决
- Shell中判断各种比较
- IM类app业务模块功能接口定义(三)
- php 的5 大 新特性!
- 央行发文放开银行远程开户,音视频软件迎契机
- runloop详解(一)
- drools 规约文件 —— 语法
- Gradle
- 判断整数是否是素数
- 【转】ibatis中使用like模糊查询