-
gt
n1
大于
n2 (greater than)
-
lt
n1
小于
n2 (less than)
-
ge
n1
大于等于
n2 (greater than or equal)
-
le
n1
小于等于
n2 (less than or equal)
5.
判定字符串的数据
test
-
z string
判定字符串是否为
0
?若
string
为空字符串,则为
true
test
-
n string
判定字符串是否非为
0
?若
string
为空字符串,则为
false
注:
-
n
亦可省略
test str1 == str2
判定
str1
是否等于
str2
,若相等,则回传
true
test str1 != str2
判定
str1
是否不等于
str2
,若相等,则回传
false
6.
多重条件判定,例如:
test
-
r filename
-
a
-
x filename
-
a
(and)
两状况同时成立!例如
test
-
r file
-
a
-
x file
,则
file
同时具有
r
x
权限
时,才回传
true
-
o
(or)
两状况任何一个成立!例如
test
-
r file
-
o
-
x file
,则
file
具有
r
x
权限时,
就可回传
true
!
反相状态,如
test !
-
x file
,当
file
不具有
x
时,回传
true
OK
!现在我们就利用
test
来帮我们写几个简单的例子。首先,判断一下,让使用者输入一个档名,
我们判断:
1.
这个文件是否存在,若不存在则给予一个『
Filename does not exist
』的讯息,并中断程序;
2.
若这个文件存在,则判断他是个文件或目录,½果输出『
Filename is regular file
』或
Filename is directory
3.
判断一下,执行者的身份对这个文件或目录所拥有的权限,并输出权限数据!
你可以先自行创作看看,然后再跟底下的½果讨论讨论。注意利用
test
&&
还有
||
等标志!
[dmtsai@study bin]$
vim file_perm.sh
#!/bin/bash
# Program:
#
User input a filename, program will check the flowing:
#
1.) exist. 2.) file/directory. 3.) file permissions
# History:
# 2015/07/16
VBird
First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin