Shell逻辑运算符及表达式

来源:互联网 发布:tcp和udp的默认端口 编辑:程序博客网 时间:2024/04/30 16:00

一、条件运算符

1. 条件运算符

运算符号代表意义应用说明=等于整型或字符串比较: str1 = str2字符串str1 和字符串str2 相等时返回真,如果在[]中,只能是字符串==等于整型或字符串比较: str1 == str2字符串str1 和字符串str2 相等时返回真,如果在[]中,只能是字符串!=不等于整型或字符串比较: str1 != str2字符串str1和字符串str2不相等时返回真,如果在[]中,只能是字符串<小于整型或字符串比较: str1 < str2按字典顺序排序,字符串str1 在字符串str2 之前,在[]中,它表示字符串,如需使用请转义\<>大于整型和字符串比较在[]中,它表示字符串,如需使用请转义\>-eq等于整型比较: int1 -eq int2如果int1 等于int2,则返回真-ne不等于整型比较: int1 -ne int2如果int1 不等于int2,则返回真-lt小于整型比较: int1 -lt int2如果int1 小于int2,则返回真-gt大于整型比较: int1 -gt int2如果int1 大于int2,则返回真-le小于或等于整型比较: int1 -le int2如果int1 小于等于int2,则返回真-ge大于或等于整型比较: int1 -ge int2如果int1 大于等于int2,则返回真-z空字符串字符串比较: -z string字符串string 为空串(长度为0)时返回真-n非空字符串字符串比较 :-n string字符串string 为非空串时返回真

2. 逻辑运算符

运算符号代表意义应用说明-a双方都成立(and)逻辑表达式 –a 逻辑表达式在[] 表达式中使用-o单方成立(or)逻辑表达式 –o 逻辑表达式在[] 表达式中使用!逻辑否,条件为假,结果为真。  &&双方都成立(and)逻辑表达式 && 逻辑表达式在[[]] 表达式中使用\\ 单方成立(or)

3. 文件和目录的判断

逻辑符号代表意义应用说明-f判断文件是否存在-f filename当filename 存在并且是正规文件时返回真-d判断目录是否存在-d pathname当pathname 存在并且是一个目录时返回真-b判断是否为一个【block档案】-b filename当filename 存在并且是块文件时返回真(返回0)-c判断是否为一个[character档案]-c filename当filename 存在并且是字符文件时返回真-S判断是否为一个[socket 标签档案]-S filename当filename 存在并且是socket 时返回真-L判断是否为一个[symbolic link 的档案]-L filename当filename 存在并且是符号链接文件时返回真-e判断【某个东西】是否存在-e pathname当由pathname 指定的文件或目录存在时返回真

4. 程序的逻辑卷标判断

逻辑符号代表意义应用说明-G判断是否由 GID 所执行的程序所拥有-G pathname当由pathname 存在并且属于当前进程的有效用户id 的用户的用户组时返回真-O判断是否由 UID 所执行的程序所拥有-O pathname当由pathname 存在并且被当前进程的有效用户id 的用户拥有时返回真(字母O 大写)-p判断是否为程序间传送信息的 name pipe 或是 FIFO-p filename当filename 存在并且是命名管道时返回真

5. 档案的属性判断

逻辑符号代表意义应用说明-r判断是否为可读的属性-r pathname当由pathname 指定的文件或目录存在并且可读时返回真-w判断是否为可以写入的属性-w pathname当由pathname 指定的文件或目录存在并且可写时返回真-x判断是否为可执行的属性-x pathname当由pathname 指定的文件或目录存在并且可执行时返回真-s判断是否为『非空白档案』-s filename当filename 存在并且文件大小大于0 时返回真-u判断是否具有『 SUID 』的属性-u pathname当由pathname 指定的文件或目录存在并且设置了SUID 位时返回真-g判断是否具有『 SGID 』的属性-g pathname当由pathname 指定的文件或目录存在并且设置了SGID 位时返回真-k判断是否具有『 sticky bit 』的属性-k pathname当由pathname 指定的文件或目录存在并且设置了"粘滞"位时返回真

6.两个档案之间的判断与比较

逻辑符号代表意义应用说明-nt第一个档案比第二个档案新file1 -nt file2file1 比file2 新时返回真-ot第一个档案比第二个档案旧file1 -ot file2file1 比file2 旧时返回真-ef第一个档案与第二个档案为同一个档案( link 之类的档案)f1 -ef f2files f1 and f2 are hard links to the same file

二. 逻辑表达式+运算符举例说明

#! bin/bash# -------------------------------------------------------------------------------# 文件名:  Shell学习总结之逻辑运算符及表达式.sh# 版 本:   1.0# 创建日期: 2014/02/23# 描 述:   逻辑运算符和逻辑表达式学习总结# 作 者:   毕小朋# 邮 箱:   wirelessqa.me@gmail.com# 博 客:   http://blog.csdn.net/wirelessqa# -------------------------------------------------------------------------------website="http://blog.csdn.net/wirelessqa"myname="bixiaopeng"echo "========= 逻辑表达式 test ========="#注意:所有字符与逻辑运算符直接用“空格”分开,不能连到一起。if test 3 -eq 3 -a 3 == 3 ;then echo "true" ;fi#当3 大于 2 或 4 大于 3 并且 bxp 不等于 bixiaopeng  或 变量website不为空时,为真if test 3 > 2 -a 4 -gt 2 -a "bxp" != "bixiaopeng" -o -n "$website" ;then echo "true"; else echo "false"; fi#判断文件是否存在if test -f "/Users/bixiaopeng/justtest.txt" ;then echo "true"; else echo "false"; fi#判断目录是否存在if test -d "/Users/bixiaopeng" ;then echo "true"; else echo "false"; fiecho "========= 逻辑表达式 [] ========="#在[] 表达式中,常见的>,<需要加转义字符,表示字符串大小比较,以acill码位置作为比较。#不直接支持<>运算符,还有逻辑运算符 || 和 && 它需要用-a[and] –o[or]表示。if [ 3 -eq 3 -a 3 == 3 ];then echo "true" ;fi#当3 大于 2 或 4 大于 3 并且 bxp 不等于 bixiaopeng  或 变量website不为空时,为真if [ 3 \> 2 -a 4 -gt 2 -a "bxp" != "bixiaopeng" -o -n "$website" ] ;then echo "true"; else echo "false"; fi#判断文件是否存在if [ -f "/Users/bixiaopeng/justtest.txt" ] ;then echo "true"; else echo "false"; fi#判断目录是否存在if [ -d "/Users/bixiaopeng" ] ;then echo "true"; else echo "false"; fiecho "========= 逻辑表达式 [[]] ========="#[[]] 运算符只是[]运算符的扩充。能够支持<,>符号运算不需要转义符,它还是以字符串比较大小。里面支持逻辑运算符 || 和 &&if [[ 3 -eq 3 && 3 == 3 ]];then echo "true" ;fi#当3 大于 2 或 4 大于 3 并且 bxp 不等于 bixiaopeng  或 变量website不为空时,为真if [[ 3 > 2 && 4 -gt 2 && "bxp" != "bixiaopeng" || -n "$website" ]] ;then echo "true"; else echo "false"; fi#判断文件是否存在if [[ -f "/Users/bixiaopeng/justtest.txt" ]] ;then echo "true"; else echo "false"; fi#判断目录是否存在if [[ -d "/Users/bixiaopeng" ]] ;then echo "true"; else echo "false"; fi#[[]] 中可以使用通配符,不需要引号[[ $myname = b*peng ]] && echo "true"


一. 运算符总结说明


1. 条件运算符

运算符号代表意义应用说明=等于整型或字符串比较: str1 = str2字符串str1 和字符串str2 相等时返回真,如果在[]中,只能是字符串==等于整型或字符串比较: str1 == str2字符串str1 和字符串str2 相等时返回真,如果在[]中,只能是字符串!=不等于整型或字符串比较: str1 != str2字符串str1和字符串str2不相等时返回真,如果在[]中,只能是字符串<小于整型或字符串比较: str1 < str2按字典顺序排序,字符串str1 在字符串str2 之前,在[]中,它表示字符串,如需使用请转义\<>大于整型和字符串比较在[]中,它表示字符串,如需使用请转义\>-eq等于整型比较: int1 -eq int2如果int1 等于int2,则返回真-ne不等于整型比较: int1 -ne int2如果int1 不等于int2,则返回真-lt小于整型比较: int1 -lt int2如果int1 小于int2,则返回真-gt大于整型比较: int1 -gt int2如果int1 大于int2,则返回真-le小于或等于整型比较: int1 -le int2如果int1 小于等于int2,则返回真-ge大于或等于整型比较: int1 -ge int2如果int1 大于等于int2,则返回真-z空字符串字符串比较: -z string字符串string 为空串(长度为0)时返回真-n非空字符串字符串比较 :-n string字符串string 为非空串时返回真

2. 逻辑运算符

运算符号代表意义应用说明-a双方都成立(and)逻辑表达式 –a 逻辑表达式在[] 表达式中使用-o单方成立(or)逻辑表达式 –o 逻辑表达式在[] 表达式中使用!逻辑否,条件为假,结果为真。  &&双方都成立(and)逻辑表达式 && 逻辑表达式在[[]] 表达式中使用\\ 单方成立(or)

3. 文件和目录的判断

逻辑符号代表意义应用说明-f判断文件是否存在-f filename当filename 存在并且是正规文件时返回真-d判断目录是否存在-d pathname当pathname 存在并且是一个目录时返回真-b判断是否为一个【block档案】-b filename当filename 存在并且是块文件时返回真(返回0)-c判断是否为一个[character档案]-c filename当filename 存在并且是字符文件时返回真-S判断是否为一个[socket 标签档案]-S filename当filename 存在并且是socket 时返回真-L判断是否为一个[symbolic link 的档案]-L filename当filename 存在并且是符号链接文件时返回真-e判断【某个东西】是否存在-e pathname当由pathname 指定的文件或目录存在时返回真

4. 程序的逻辑卷标判断

逻辑符号代表意义应用说明-G判断是否由 GID 所执行的程序所拥有-G pathname当由pathname 存在并且属于当前进程的有效用户id 的用户的用户组时返回真-O判断是否由 UID 所执行的程序所拥有-O pathname当由pathname 存在并且被当前进程的有效用户id 的用户拥有时返回真(字母O 大写)-p判断是否为程序间传送信息的 name pipe 或是 FIFO-p filename当filename 存在并且是命名管道时返回真

5. 档案的属性判断

逻辑符号代表意义应用说明-r判断是否为可读的属性-r pathname当由pathname 指定的文件或目录存在并且可读时返回真-w判断是否为可以写入的属性-w pathname当由pathname 指定的文件或目录存在并且可写时返回真-x判断是否为可执行的属性-x pathname当由pathname 指定的文件或目录存在并且可执行时返回真-s判断是否为『非空白档案』-s filename当filename 存在并且文件大小大于0 时返回真-u判断是否具有『 SUID 』的属性-u pathname当由pathname 指定的文件或目录存在并且设置了SUID 位时返回真-g判断是否具有『 SGID 』的属性-g pathname当由pathname 指定的文件或目录存在并且设置了SGID 位时返回真-k判断是否具有『 sticky bit 』的属性-k pathname当由pathname 指定的文件或目录存在并且设置了"粘滞"位时返回真

6.两个档案之间的判断与比较

逻辑符号代表意义应用说明-nt第一个档案比第二个档案新file1 -nt file2file1 比file2 新时返回真-ot第一个档案比第二个档案旧file1 -ot file2file1 比file2 旧时返回真-ef第一个档案与第二个档案为同一个档案( link 之类的档案)f1 -ef f2files f1 and f2 are hard links to the same file

二. 逻辑表达式+运算符举例说明

#! bin/bash# -------------------------------------------------------------------------------# 文件名:  Shell学习总结之逻辑运算符及表达式.sh# 版 本:   1.0# 创建日期: 2014/02/23# 描 述:   逻辑运算符和逻辑表达式学习总结# 作 者:   毕小朋# 邮 箱:   wirelessqa.me@gmail.com# 博 客:   http://blog.csdn.net/wirelessqa# -------------------------------------------------------------------------------website="http://blog.csdn.net/wirelessqa"myname="bixiaopeng"echo "========= 逻辑表达式 test ========="#注意:所有字符与逻辑运算符直接用“空格”分开,不能连到一起。if test 3 -eq 3 -a 3 == 3 ;then echo "true" ;fi#当3 大于 2 或 4 大于 3 并且 bxp 不等于 bixiaopeng  或 变量website不为空时,为真if test 3 > 2 -a 4 -gt 2 -a "bxp" != "bixiaopeng" -o -n "$website" ;then echo "true"; else echo "false"; fi#判断文件是否存在if test -f "/Users/bixiaopeng/justtest.txt" ;then echo "true"; else echo "false"; fi#判断目录是否存在if test -d "/Users/bixiaopeng" ;then echo "true"; else echo "false"; fiecho "========= 逻辑表达式 [] ========="#在[] 表达式中,常见的>,<需要加转义字符,表示字符串大小比较,以acill码位置作为比较。#不直接支持<>运算符,还有逻辑运算符 || 和 && 它需要用-a[and] –o[or]表示。if [ 3 -eq 3 -a 3 == 3 ];then echo "true" ;fi#当3 大于 2 或 4 大于 3 并且 bxp 不等于 bixiaopeng  或 变量website不为空时,为真if [ 3 \> 2 -a 4 -gt 2 -a "bxp" != "bixiaopeng" -o -n "$website" ] ;then echo "true"; else echo "false"; fi#判断文件是否存在if [ -f "/Users/bixiaopeng/justtest.txt" ] ;then echo "true"; else echo "false"; fi#判断目录是否存在if [ -d "/Users/bixiaopeng" ] ;then echo "true"; else echo "false"; fiecho "========= 逻辑表达式 [[]] ========="#[[]] 运算符只是[]运算符的扩充。能够支持<,>符号运算不需要转义符,它还是以字符串比较大小。里面支持逻辑运算符 || 和 &&if [[ 3 -eq 3 && 3 == 3 ]];then echo "true" ;fi#当3 大于 2 或 4 大于 3 并且 bxp 不等于 bixiaopeng  或 变量website不为空时,为真if [[ 3 > 2 && 4 -gt 2 && "bxp" != "bixiaopeng" || -n "$website" ]] ;then echo "true"; else echo "false"; fi#判断文件是否存在if [[ -f "/Users/bixiaopeng/justtest.txt" ]] ;then echo "true"; else echo "false"; fi#判断目录是否存在if [[ -d "/Users/bixiaopeng" ]] ;then echo "true"; else echo "false"; fi#[[]] 中可以使用通配符,不需要引号[[ $myname = b*peng ]] && echo "true"


一. 运算符总结说明


1. 条件运算符

运算符号代表意义应用说明=等于整型或字符串比较: str1 = str2字符串str1 和字符串str2 相等时返回真,如果在[]中,只能是字符串==等于整型或字符串比较: str1 == str2字符串str1 和字符串str2 相等时返回真,如果在[]中,只能是字符串!=不等于整型或字符串比较: str1 != str2字符串str1和字符串str2不相等时返回真,如果在[]中,只能是字符串<小于整型或字符串比较: str1 < str2按字典顺序排序,字符串str1 在字符串str2 之前,在[]中,它表示字符串,如需使用请转义\<>大于整型和字符串比较在[]中,它表示字符串,如需使用请转义\>-eq等于整型比较: int1 -eq int2如果int1 等于int2,则返回真-ne不等于整型比较: int1 -ne int2如果int1 不等于int2,则返回真-lt小于整型比较: int1 -lt int2如果int1 小于int2,则返回真-gt大于整型比较: int1 -gt int2如果int1 大于int2,则返回真-le小于或等于整型比较: int1 -le int2如果int1 小于等于int2,则返回真-ge大于或等于整型比较: int1 -ge int2如果int1 大于等于int2,则返回真-z空字符串字符串比较: -z string字符串string 为空串(长度为0)时返回真-n非空字符串字符串比较 :-n string字符串string 为非空串时返回真

2. 逻辑运算符

运算符号代表意义应用说明-a双方都成立(and)逻辑表达式 –a 逻辑表达式在[] 表达式中使用-o单方成立(or)逻辑表达式 –o 逻辑表达式在[] 表达式中使用!逻辑否,条件为假,结果为真。  &&双方都成立(and)逻辑表达式 && 逻辑表达式在[[]] 表达式中使用\\ 单方成立(or)

3. 文件和目录的判断

逻辑符号代表意义应用说明-f判断文件是否存在-f filename当filename 存在并且是正规文件时返回真-d判断目录是否存在-d pathname当pathname 存在并且是一个目录时返回真-b判断是否为一个【block档案】-b filename当filename 存在并且是块文件时返回真(返回0)-c判断是否为一个[character档案]-c filename当filename 存在并且是字符文件时返回真-S判断是否为一个[socket 标签档案]-S filename当filename 存在并且是socket 时返回真-L判断是否为一个[symbolic link 的档案]-L filename当filename 存在并且是符号链接文件时返回真-e判断【某个东西】是否存在-e pathname当由pathname 指定的文件或目录存在时返回真

4. 程序的逻辑卷标判断

逻辑符号代表意义应用说明-G判断是否由 GID 所执行的程序所拥有-G pathname当由pathname 存在并且属于当前进程的有效用户id 的用户的用户组时返回真-O判断是否由 UID 所执行的程序所拥有-O pathname当由pathname 存在并且被当前进程的有效用户id 的用户拥有时返回真(字母O 大写)-p判断是否为程序间传送信息的 name pipe 或是 FIFO-p filename当filename 存在并且是命名管道时返回真

5. 档案的属性判断

逻辑符号代表意义应用说明-r判断是否为可读的属性-r pathname当由pathname 指定的文件或目录存在并且可读时返回真-w判断是否为可以写入的属性-w pathname当由pathname 指定的文件或目录存在并且可写时返回真-x判断是否为可执行的属性-x pathname当由pathname 指定的文件或目录存在并且可执行时返回真-s判断是否为『非空白档案』-s filename当filename 存在并且文件大小大于0 时返回真-u判断是否具有『 SUID 』的属性-u pathname当由pathname 指定的文件或目录存在并且设置了SUID 位时返回真-g判断是否具有『 SGID 』的属性-g pathname当由pathname 指定的文件或目录存在并且设置了SGID 位时返回真-k判断是否具有『 sticky bit 』的属性-k pathname当由pathname 指定的文件或目录存在并且设置了"粘滞"位时返回真

6.两个档案之间的判断与比较

逻辑符号代表意义应用说明-nt第一个档案比第二个档案新file1 -nt file2file1 比file2 新时返回真-ot第一个档案比第二个档案旧file1 -ot file2file1 比file2 旧时返回真-ef第一个档案与第二个档案为同一个档案( link 之类的档案)f1 -ef f2files f1 and f2 are hard links to the same file

二. 逻辑表达式+运算符举例说明

#! bin/bash# -------------------------------------------------------------------------------# 文件名:  Shell学习总结之逻辑运算符及表达式.sh# 版 本:   1.0# 创建日期: 2014/02/23# 描 述:   逻辑运算符和逻辑表达式学习总结# 作 者:   毕小朋# 邮 箱:   wirelessqa.me@gmail.com# 博 客:   http://blog.csdn.net/wirelessqa# -------------------------------------------------------------------------------website="http://blog.csdn.net/wirelessqa"myname="bixiaopeng"echo "========= 逻辑表达式 test ========="#注意:所有字符与逻辑运算符直接用“空格”分开,不能连到一起。if test 3 -eq 3 -a 3 == 3 ;then echo "true" ;fi#当3 大于 2 或 4 大于 3 并且 bxp 不等于 bixiaopeng  或 变量website不为空时,为真if test 3 > 2 -a 4 -gt 2 -a "bxp" != "bixiaopeng" -o -n "$website" ;then echo "true"; else echo "false"; fi#判断文件是否存在if test -f "/Users/bixiaopeng/justtest.txt" ;then echo "true"; else echo "false"; fi#判断目录是否存在if test -d "/Users/bixiaopeng" ;then echo "true"; else echo "false"; fiecho "========= 逻辑表达式 [] ========="#在[] 表达式中,常见的>,<需要加转义字符,表示字符串大小比较,以acill码位置作为比较。#不直接支持<>运算符,还有逻辑运算符 || 和 && 它需要用-a[and] –o[or]表示。if [ 3 -eq 3 -a 3 == 3 ];then echo "true" ;fi#当3 大于 2 或 4 大于 3 并且 bxp 不等于 bixiaopeng  或 变量website不为空时,为真if [ 3 \> 2 -a 4 -gt 2 -a "bxp" != "bixiaopeng" -o -n "$website" ] ;then echo "true"; else echo "false"; fi#判断文件是否存在if [ -f "/Users/bixiaopeng/justtest.txt" ] ;then echo "true"; else echo "false"; fi#判断目录是否存在if [ -d "/Users/bixiaopeng" ] ;then echo "true"; else echo "false"; fiecho "========= 逻辑表达式 [[]] ========="#[[]] 运算符只是[]运算符的扩充。能够支持<,>符号运算不需要转义符,它还是以字符串比较大小。里面支持逻辑运算符 || 和 &&if [[ 3 -eq 3 && 3 == 3 ]];then echo "true" ;fi#当3 大于 2 或 4 大于 3 并且 bxp 不等于 bixiaopeng  或 变量website不为空时,为真if [[ 3 > 2 && 4 -gt 2 && "bxp" != "bixiaopeng" || -n "$website" ]] ;then echo "true"; else echo "false"; fi#判断文件是否存在if [[ -f "/Users/bixiaopeng/justtest.txt" ]] ;then echo "true"; else echo "false"; fi#判断目录是否存在if [[ -d "/Users/bixiaopeng" ]] ;then echo "true"; else echo "false"; fi#[[]] 中可以使用通配符,不需要引号[[ $myname = b*peng ]] && echo "true"

1. 条件运算符

运算符号代表意义应用说明=等于整型或字符串比较: str1 = str2字符串str1 和字符串str2 相等时返回真,如果在[]中,只能是字符串==等于整型或字符串比较: str1 == str2字符串str1 和字符串str2 相等时返回真,如果在[]中,只能是字符串!=不等于整型或字符串比较: str1 != str2字符串str1和字符串str2不相等时返回真,如果在[]中,只能是字符串<小于整型或字符串比较: str1 < str2按字典顺序排序,字符串str1 在字符串str2 之前,在[]中,它表示字符串,如需使用请转义\<>大于整型和字符串比较在[]中,它表示字符串,如需使用请转义\>-eq等于整型比较: int1 -eq int2如果int1 等于int2,则返回真-ne不等于整型比较: int1 -ne int2如果int1 不等于int2,则返回真-lt小于整型比较: int1 -lt int2如果int1 小于int2,则返回真-gt大于整型比较: int1 -gt int2如果int1 大于int2,则返回真-le小于或等于整型比较: int1 -le int2如果int1 小于等于int2,则返回真-ge大于或等于整型比较: int1 -ge int2如果int1 大于等于int2,则返回真-z空字符串字符串比较: -z string字符串string 为空串(长度为0)时返回真-n非空字符串字符串比较 :-n string字符串string 为非空串时返回真

2. 逻辑运算符

运算符号代表意义应用说明-a双方都成立(and)逻辑表达式 –a 逻辑表达式在[] 表达式中使用-o单方成立(or)逻辑表达式 –o 逻辑表达式在[] 表达式中使用!逻辑否,条件为假,结果为真。  &&双方都成立(and)逻辑表达式 && 逻辑表达式在[[]] 表达式中使用\\ 单方成立(or)

3. 文件和目录的判断

逻辑符号代表意义应用说明-f判断文件是否存在-f filename当filename 存在并且是正规文件时返回真-d判断目录是否存在-d pathname当pathname 存在并且是一个目录时返回真-b判断是否为一个【block档案】-b filename当filename 存在并且是块文件时返回真(返回0)-c判断是否为一个[character档案]-c filename当filename 存在并且是字符文件时返回真-S判断是否为一个[socket 标签档案]-S filename当filename 存在并且是socket 时返回真-L判断是否为一个[symbolic link 的档案]-L filename当filename 存在并且是符号链接文件时返回真-e判断【某个东西】是否存在-e pathname当由pathname 指定的文件或目录存在时返回真

4. 程序的逻辑卷标判断

逻辑符号代表意义应用说明-G判断是否由 GID 所执行的程序所拥有-G pathname当由pathname 存在并且属于当前进程的有效用户id 的用户的用户组时返回真-O判断是否由 UID 所执行的程序所拥有-O pathname当由pathname 存在并且被当前进程的有效用户id 的用户拥有时返回真(字母O 大写)-p判断是否为程序间传送信息的 name pipe 或是 FIFO-p filename当filename 存在并且是命名管道时返回真

5. 档案的属性判断

逻辑符号代表意义应用说明-r判断是否为可读的属性-r pathname当由pathname 指定的文件或目录存在并且可读时返回真-w判断是否为可以写入的属性-w pathname当由pathname 指定的文件或目录存在并且可写时返回真-x判断是否为可执行的属性-x pathname当由pathname 指定的文件或目录存在并且可执行时返回真-s判断是否为『非空白档案』-s filename当filename 存在并且文件大小大于0 时返回真-u判断是否具有『 SUID 』的属性-u pathname当由pathname 指定的文件或目录存在并且设置了SUID 位时返回真-g判断是否具有『 SGID 』的属性-g pathname当由pathname 指定的文件或目录存在并且设置了SGID 位时返回真-k判断是否具有『 sticky bit 』的属性-k pathname当由pathname 指定的文件或目录存在并且设置了"粘滞"位时返回真

6.两个档案之间的判断与比较

逻辑符号代表意义应用说明-nt第一个档案比第二个档案新file1 -nt file2file1 比file2 新时返回真-ot第一个档案比第二个档案旧file1 -ot file2file1 比file2 旧时返回真-ef第一个档案与第二个档案为同一个档案( link 之类的档案)f1 -ef f2files f1 and f2 are hard links to the same file

二. 逻辑表达式+运算符举例说明

#! bin/bash# -------------------------------------------------------------------------------# 文件名:  Shell学习总结之逻辑运算符及表达式.sh# 版 本:   1.0# 创建日期: 2014/02/23# 描 述:   逻辑运算符和逻辑表达式学习总结# 作 者:   毕小朋# 邮 箱:   wirelessqa.me@gmail.com# 博 客:   http://blog.csdn.net/wirelessqa# -------------------------------------------------------------------------------website="http://blog.csdn.net/wirelessqa"myname="bixiaopeng"echo "========= 逻辑表达式 test ========="#注意:所有字符与逻辑运算符直接用“空格”分开,不能连到一起。if test 3 -eq 3 -a 3 == 3 ;then echo "true" ;fi#当3 大于 2 或 4 大于 3 并且 bxp 不等于 bixiaopeng  或 变量website不为空时,为真if test 3 > 2 -a 4 -gt 2 -a "bxp" != "bixiaopeng" -o -n "$website" ;then echo "true"; else echo "false"; fi#判断文件是否存在if test -f "/Users/bixiaopeng/justtest.txt" ;then echo "true"; else echo "false"; fi#判断目录是否存在if test -d "/Users/bixiaopeng" ;then echo "true"; else echo "false"; fiecho "========= 逻辑表达式 [] ========="#在[] 表达式中,常见的>,<需要加转义字符,表示字符串大小比较,以acill码位置作为比较。#不直接支持<>运算符,还有逻辑运算符 || 和 && 它需要用-a[and] –o[or]表示。if [ 3 -eq 3 -a 3 == 3 ];then echo "true" ;fi#当3 大于 2 或 4 大于 3 并且 bxp 不等于 bixiaopeng  或 变量website不为空时,为真if [ 3 \> 2 -a 4 -gt 2 -a "bxp" != "bixiaopeng" -o -n "$website" ] ;then echo "true"; else echo "false"; fi#判断文件是否存在if [ -f "/Users/bixiaopeng/justtest.txt" ] ;then echo "true"; else echo "false"; fi#判断目录是否存在if [ -d "/Users/bixiaopeng" ] ;then echo "true"; else echo "false"; fiecho "========= 逻辑表达式 [[]] ========="#[[]] 运算符只是[]运算符的扩充。能够支持<,>符号运算不需要转义符,它还是以字符串比较大小。里面支持逻辑运算符 || 和 &&if [[ 3 -eq 3 && 3 == 3 ]];then echo "true" ;fi#当3 大于 2 或 4 大于 3 并且 bxp 不等于 bixiaopeng  或 变量website不为空时,为真if [[ 3 > 2 && 4 -gt 2 && "bxp" != "bixiaopeng" || -n "$website" ]] ;then echo "true"; else echo "false"; fi#判断文件是否存在if [[ -f "/Users/bixiaopeng/justtest.txt" ]] ;then echo "true"; else echo "false"; fi#判断目录是否存在if [[ -d "/Users/bixiaopeng" ]] ;then echo "true"; else echo "false"; fi#[[]] 中可以使用通配符,不需要引号[[ $myname = b*peng ]] && echo "true"
Shell的制表符为 \x09

阅读全文
0 0
原创粉丝点击