[-a file] 如果file存在则为真
不过貌似有时候-a表示为and:条件与
[-b file] 如果file存在且是一个块特殊文件则为真
[-c file] 如果file存在且是一个字特殊文件则为真
[-d file] 如果file文件存在且是一个目录则为真
-d前的!是逻辑非
例如:
if [ ! -d $lcd_path/$par_date ]
表示后面的那个目录不存在,则执行后面的then操作
[-e file] 如果file文件存在则为真
[-f file] 如果file存在且是一个普通文件则为真
[-g file] 如果file存在且已经设置了SGID则为真(SUID 是 Set User ID, SGID 是 Set Group ID的意思)
[-h file] 如果file存在且是一个符号连接则为真
[-k file] 如果file存在且已经设置粘制位则为真
当一个目录被设置为"粘制位"(用chmod a+t),则该目录下的文件只能由
一、超级管理员删除
二、该目录的所有者删除
三、该文件的所有者删除
也就是说,即便该目录是任何人都可以写,但也只有文件的属主才可以删除文件。
具体例子如下:
#ls -dl /tmp
drwxrwxrwt 4 root root .........
注意other位置的t,这便是粘连位。
[-p file] 如果file存在且是一个名字管道(F如果O)则为真
管道是linux里面进程间通信的一种方式,其他的还有像信号(signal)、信号量、消息队列、共享内存、套接字(socket)等。
[-r file] 如果file存在且是可读的则为真
[-s file] 如果file存在且大小不为0则为真
[-t FD] 如果文件描述符FD打开且指向一个终端则为真
[-u file] 如果file存在且设置了SUID(set userID)则为真
[-w file] 如果file存在且是可写的则为真
[-x file] 如果file存在且是可执行的则为真
[-O file] 如果file存在且属有效用户ID则为真
[-G file] 如果file存在且属有效用户组则为真
[-L file] 如果file存在且是一个符号连接则为真
[-N file] 如果file存在and has been mod如果ied since it was last read则为真
[-S file] 如果file存在且是一个套接字则为真
[file1 –nt file2] 如果file1 has been changed more recently than file2或者file1 exists and file2 does not则为真
[file1 –ot file2] 如果file1比file2要老,或者file2存在且file1不存在则为真
[file1 –ef file2] 如果file1和file2指向相同的设备和节点号则为真
[-o optionname] 如果shell选项“optionname”开启则为真
[-z string] “string”的长度为零则为真
[-n string] or [string] “string”的长度为非零non-zero则为真
[sting1==string2] 如果2个字符串相同。“=”may be used instead of “==”for strict posix compliance则为真
[string1!=string2] 如果字符串不相等则为真
[string1<string2] 如果“string1”sorts before“string2”lexicographically in the current locale则为真
[arg1 OP arg2] “OP”is one of –eq,-ne,-lt,-le,-gt or –ge.These arithmetic binary oprators return true if “arg1”is equal to,not equal to,less than,less than or equal to,greater than,or greater than or equal to“agr2”,respectively.“arg1”and “agr2”are integers.
===========================================================================
常用:
shell 编程中使用到得if语句内判断参数
–b 当file存在并且是块文件时返回真
-c 当file存在并且是字符文件时返回真
-d 当pathname存在并且是一个目录时返回真
-e 当pathname指定的文件或目录存在时返回真
-f 当file存在并且是正规文件时返回真
-g 当由pathname指定的文件或目录存在并且设置了SGID位时返回为真
-h 当file存在并且是符号链接文件时返回真,该选项在一些老系统上无效
-k 当由pathname指定的文件或目录存在并且设置了“粘滞”位时返回真
-p 当file存在并且是命令管道时返回为真
-r 当由pathname指定的文件或目录存在并且可读时返回为真
-s 当file存在文件大小大于0时返回真
-u 当由pathname指定的文件或目录存在并且设置了SUID位时返回真
-w 当由pathname指定的文件或目录存在并且可执行时返回真。一个目录为了它的内容被访问必然是可执行的。
-o 当由pathname指定的文件或目录存在并且被子当前进程的有效用户ID所指定的用户拥有时返回真。
UNIX Shell 里面比较字符写法:
-eq 等于
-ne 不等于
-gt 大于
-lt 小于
-le 小于等于
-ge 大于等于
-z 空串
= 两个字符相等
!= 两个字符不等
-n 非空串
-------------------------------------------------------------------------
更为详细的说明:
运算符 描述 示例
文件比较运算符
-e filename 如果 filename 存在,则为真 [ -e /var/log/syslog ]
-d filename 如果 filename 为目录,则为真 [ -d /tmp/mydir ]
-f filename 如果 filename 为常规文件,则为真 [ -f /usr/bin/grep ]
-L filename 如果 filename 为符号链接,则为真 [ -L /usr/bin/grep ]
-r filename 如果 filename 可读,则为真 [ -r /var/log/syslog ]
-w filename 如果 filename 可写,则为真 [ -w /var/mytmp.txt ]
-x filename 如果 filename 可执行,则为真 [ -L /usr/bin/grep ]
filename1 -nt filename2 如果 filename1 比 filename2 新,则为真 [ /tmp/install/etc/services -nt /etc/services ]
filename1 -ot filename2 如果 filename1 比 filename2 旧,则为真 [ /boot/bzImage -ot arch/i386/boot/bzImage ]
字符串比较运算符 (请注意引号的使用,这是防止空格扰乱代码的好方法)
-z string 如果 string 长度为零,则为真 [ -z $myvar ]
-n string 如果 string 长度非零,则为真 [ -n $myvar ]
string1 = string2 如果 string1 与 string2 相同,则为真 [ $myvar = one two three ]
string1 != string2 如果 string1 与 string2 不同,则为真 [ $myvar != one two three ]
算术比较运算符
num1 -eq num2 等于 [ 3 -eq $mynum ]
num1 -ne num2 不等于 [ 3 -ne $mynum ]
num1 -lt num2 小于 [ 3 -lt $mynum ]
num1 -le num2 小于或等于 [ 3 -le $mynum ]
num1 -gt num2 大于 [ 3 -gt $mynum ]
num1 -ge num2 大于或等于 [ 3 -ge $mynum ]
脚本示例:
#!/bin/bash
# This script prints a message about your weight if you give it your
# weight in kilos and hight in centimeters.
if [ ! $# == 2 ]; then
echo "Usage: $0 weight_in_kilos length_in_centimeters"
exit
fi
weight="$1"
height="$2"
idealweight=$[$height - 110]
if [ $weight -le $idealweight ] ; then
echo "You should eat a bit more fat."
else
echo "You should eat a bit more fruit."
fi
# weight.sh 70 150
You should eat a bit more fruit.
# weight.sh 70 150 33
Usage: ./weight.sh weight_in_kilos length_in_centimeters
位置参数 $1, $2,..., $N,$#代表了命令行的参数数量, $0代表了脚本的名字,
第一个参数代表$1,第二个参数代表$2,以此类推,参数数量的总数存在$#中,上面的例子显示了怎么改变脚本,如果参数少于或者多余2个来打印出一条消息。
执行,并查看情况。
# bash -x tijian.sh 60 170
+ weight=60
+ height=170
+ idealweight=60
+ '[' 60 -le 60 ']'
+ echo 'You should eat a bit more fat.'
You should eat a bit more fat.
其中-x用来检查脚本的执行情况。