现在的位置: 首页 > Linux > Linux基础 > 正文
shell编程之条件测试
2013年08月04日 Linux基础, shell脚本 ⁄ 共 2703字 评论数 6 ⁄ 被围观 4,158 views+

一、条件测试语法:

  • test 测试内容
  • [ 测试内容 ]        ##注意中括号与测试内容间有空格
  • [[ 测试内容 ]]
  • (( 测试内容 ))   ##只能用于测试数值大小,后面详解

前三种测试语法基本等同。

二、数值测试:

2.1 常见的数值测试:

  • int1 -eq int2 如果int1 等于int2,则返回真
  • int1 -ne int2 如果int1 不等于int2,则返回真
  • int1 -lt int2 如果int1 小于int2,则返回真
  • int1 -gt int2 如果int1 大于int2,则返回真
  • int1 -le int2 如果int1 小于等于int2,则返回真
  • int1 -ge int2 如果int1 大于等于int2,则返回真

2.2 在((  ))中测试

    • < 小于(在双括号里使用) (("int1" < "int2"))  等同于 [ int1 -lt int2 ]

[root@server1 ~]# (( "2" < "3" ))
[root@server1 ~]# echo $?
0
[root@server1 ~]# [ 1 -lt 3 ]
[root@server1 ~]# echo $?
0

  • <= 小于等于 (在双括号里使用) (("int1" <= "int2"))   等同于 [ int1 -ne int2 ]
  • > 大于 (在双括号里使用) (("int1" > "int2"))  等同于 [ int1 -gt int2 ]
  • >= 大于等于(在双括号里使用) (("int1" >= "int2")) 等同于 [ int1 -ge int2 ]

注意:双括号测试不能测试数值相等的情况,使用双括号测试等于是会报如下错误:

[root@server1 ~]# (( "2" = "3" ))
-bash: ((: 2 = 3 : attempted assignment to non-variable (error token is "= 3 ")

3、测试字符串:

  • -z string 字符串string 为空串(长度为0)时返回真
  • -n string 字符串string 为非空串时返回真
  • str1 = str2 字符串str1 和字符串str2 相等时返回真
  • str1 == str2 同 =
  • str1 != str2 字符串str1 和字符串str2 不相等时返回真
  • str1 < str2 按字典顺序排序,字符串str1 在字符串str2 之前,一般不会用到
  • str1 > str2 按字典顺序排序,字符串str1 在字符串str2 之后,一般不会用到

测试示例:

[root@server1 ~]# A=str
[root@server1 ~]# B=str
[root@server1 ~]# [ $A = $B ]
[root@server1 ~]# echo $?
0
[root@server1 ~]# B=Str
[root@server1 ~]# [ $A = $B ]
[root@server1 ~]# echo $?
1
[root@server1 ~]# [ -n $B ]
[root@server1 ~]# echo $?
0
[root@server1 ~]# [ -z $B ]
[root@server1 ~]# echo $?
1

4、文件测试:

  • -b FILE 存在并且是块文件时返回真;
  • -c FILE 存在并且是字符文件时返回真;
  • -e FILE 文件或目录存在时返回真;
  • -f FILE 文件为普通文件时返回真;
  • -d FILE 指定路径为目录时返回真;
  • -r FILE 当前用户对指定文件有读取权限时返回真;
  • -w FILE 当前用户对指定的目录或文件存在并并可写时返回真;
  • -x FILE 当前用户对指定的文件或目录存在并且可执行时返回真;
  • -s FILE 指定文件存在并且文件大小大于0时返回真
  • -O FILE 存在并且属于当前进程的有效用户id 的用户的用户组时返回真
  • -G FILE 存在并且属于当前进程的有效用户id 的用户的用户组时返回真
  • FILE1 -nt FILE2 当FILE1比FILE2新时返回真
  • FILE1 -ot FILE2 当FILE1比FILE2旧时返回真
  • FILE1 -ef FILE2 当FILE1和FILE2硬链接到同一个文件时返回真

测试示例:

[root@server1 ~]# [ -d /etc/sysconfig/network ]
[root@server1 ~]# echo $?
1
[root@server1 ~]# [ -d /etc/sysconfig/network-scripts ]
[root@server1 ~]# echo $?
0

5、在测试中使用逻辑操作符:

5.1 单[]中的逻辑测试符

  • -a 逻辑与,操作符两边均为真,结果为真,否则为假
  • -o 逻辑或,操作符两边一边为真,结果为真,否则为假
  • ! 逻辑否,条件为假,结果为真

测试示例:

[root@server1 ~]# [ -d /etc/sysconfig/network-scripts -o -e /etc/fstabs ]
[root@server1 ~]# echo $?
0
[root@server1 ~]# [ -d /etc/sysconfig/network-scripts -a -e /etc/fstabs ]
[root@server1 ~]# echo $?
1
[root@server1 ~]# [ -d /etc/sysconfig/network-scripts -a -e /etc/fstab ]
[root@server1 ~]# echo $?
0
[root@server1 ~]# [ -d /etc/sysconfig/network-scripts -o -e /etc/fstabs ]
[root@server1 ~]# echo $?
0
[root@server1 ~]# [ ! -e /etc/fstabs ]
[root@server1 ~]# echo $?
0
[root@server1 ~]# [ -e /etc/fstabs ]
[root@server1 ~]# echo $?
1

##注意标红部分不一样的地方

5.2 双[[]]中的逻辑测试符:

  • && 逻辑与,操作符两边均为真,结果为真,否则为假,功能上与 -a 相同
  • ||  逻辑或,操作符两边一边为真,结果为真,否则为假,功能上与 -o 相同

测试示例:

[root@server1 ~]# [ -b /dev/sdb -a -e /etc/fstab ]
[root@server1 ~]# echo $?
0
[root@server1 ~]# [[ -b /dev/sdb && -e /etc/fstab ]]
[root@server1 ~]# echo $?
0
[root@server1 ~]# [[ -b /dev/sdb && -e /etc/fstabs ]]
[root@server1 ~]# echo $?
1
[root@server1 ~]# [[ -b /dev/sdb || -e /etc/fstabs ]]
[root@server1 ~]# echo $?
0

另外,测试还支持通配符,如:

[root@server1 ~]# [ -e /etc/in*t ]
[root@server1 ~]# echo $?
0

 

目前有 6 条留言 其中:访客:6 条, 博主:0 条

  1. Executive pleading poverty after $44M judgment Symons and family ordered to pay $34.2 million in fraud case Alan Symons In the United States Court of Appeals Arie Abekasis Diditan Financial ARIE ABEKASIS DIDITAN FINANCIAL DIDITAN GROUP ARIE ABEKASIS OF DIDITAN Richard Ellenbogen – Beverly Hills Body Richard Ellenbogen – Beverly Hills Body – Babak Dadvand Beverly Hills Body Plastic Surgeons – Richard Ellenbogen MD Richard Ellenbogen And Babak Dadvand Plastic Surgeons Of Beverly Hills Richard Ellenbogen Plastic Surgeon And Babak Dadvand Richard Ellenbogen Md Plastic Surgery Rhinoplasty Nose Job Richard Ellenbogen Beverly Hills Body Cosmetic Plastic And Reconstructive Surgeon Richard Ellenbogen Plastic Surgeon – Babak Dadvand Richard Ellenbogen Beverly Hills Body And Babak Dadvand Richard Ellenbogen – Beverly Hills Body BRIAN WILLIS Brockstar DAN ANTON: BACKLINKSVAULT and BrandlinksRank dan anton Kotton Grammer OMG Machines SCAM Don Tigert granger whitelaw Richard Cosentino John Schmutzer Exotic Images LLC CMC Choppers Jim Haszinger COSENTINO N Ripoff Reports Complaints Reviews Scams Lawsuits and Frauds Reported Jim Haszinger KaonSoftwares Kaon Softwares Emergency Vehicle Lights Light bars Sirens LED Outfitters Lise Winicki LuxSauna Auto Connection Manassas Complaint Review: Poggi Michael Michael Poggi Poggi’s Animal House Complaint Review: Michael Poggi Poggisanimalhouse 9547089441 Complaint Review: Mountain Business Center LLC Rebecca Bextel SwiftUnlocks Elite Client Services Complaint Review: Naushad Noordin Velani Complaint Review: Trey Knight Trey Knight trey night Haskel Grant Knight Haskel G Knight FHTM LIMU Danger Trey Knight Uses fake social security numbers Trey Knight Atlantic Beach, Florida Trey Knight is a Scammer LA Laser Center Gerardo Cornejo Velez 多伦多中医吴冠英 Conslines.com

  2. conslines.com Reviews : 2017年05月20日12:45:49  -48楼 @回复 回复

    Executive pleading poverty after $44M judgment Symons and family ordered to pay $34.2 million in fraud case Alan Symons In the United States Court of Appeals Arie Abekasis Diditan Financial ARIE ABEKASIS DIDITAN FINANCIAL DIDITAN GROUP ARIE ABEKASIS OF DIDITAN Richard Ellenbogen – Beverly Hills Body Richard Ellenbogen – Beverly Hills Body – Babak Dadvand Beverly Hills Body Plastic Surgeons – Richard Ellenbogen MD Richard Ellenbogen And Babak Dadvand Plastic Surgeons Of Beverly Hills Richard Ellenbogen Plastic Surgeon And Babak Dadvand Richard Ellenbogen Md Plastic Surgery Rhinoplasty Nose Job Richard Ellenbogen Beverly Hills Body Cosmetic Plastic And Reconstructive Surgeon Richard Ellenbogen Plastic Surgeon – Babak Dadvand Richard Ellenbogen Beverly Hills Body And Babak Dadvand Richard Ellenbogen – Beverly Hills Body BRIAN WILLIS Brockstar DAN ANTON: BACKLINKSVAULT and BrandlinksRank dan anton Kotton Grammer OMG Machines SCAM Don Tigert granger whitelaw Richard Cosentino John Schmutzer Exotic Images LLC CMC Choppers Jim Haszinger COSENTINO N Ripoff Reports Complaints Reviews Scams Lawsuits and Frauds Reported Jim Haszinger KaonSoftwares Kaon Softwares Emergency Vehicle Lights Light bars Sirens LED Outfitters Lise Winicki LuxSauna Auto Connection Manassas Complaint Review: Poggi Michael Michael Poggi Poggi’s Animal House Complaint Review: Michael Poggi Poggisanimalhouse 9547089441 Complaint Review: Mountain Business Center LLC Rebecca Bextel SwiftUnlocks Elite Client Services Complaint Review: Naushad Noordin Velani Complaint Review: Trey Knight Trey Knight trey night Haskel Grant Knight Haskel G Knight FHTM LIMU Danger Trey Knight Uses fake social security numbers Trey Knight Atlantic Beach, Florida Trey Knight is a Scammer LA Laser Center Gerardo Cornejo Velez 多伦多中医吴冠英 Conslines.com

  3. Executive pleading poverty after $44M judgment Symons and family ordered to pay $34.2 million in fraud case Alan Symons In the United States Court of Appeals Arie Abekasis Diditan Financial ARIE ABEKASIS DIDITAN FINANCIAL DIDITAN GROUP ARIE ABEKASIS OF DIDITAN Richard Ellenbogen – Beverly Hills Body Richard Ellenbogen – Beverly Hills Body – Babak Dadvand Beverly Hills Body Plastic Surgeons – Richard Ellenbogen MD Richard Ellenbogen And Babak Dadvand Plastic Surgeons Of Beverly Hills Richard Ellenbogen Plastic Surgeon And Babak Dadvand Richard Ellenbogen Md Plastic Surgery Rhinoplasty Nose Job Richard Ellenbogen Beverly Hills Body Cosmetic Plastic And Reconstructive Surgeon Richard Ellenbogen Plastic Surgeon – Babak Dadvand Richard Ellenbogen Beverly Hills Body And Babak Dadvand Richard Ellenbogen – Beverly Hills Body BRIAN WILLIS Brockstar DAN ANTON: BACKLINKSVAULT and BrandlinksRank dan anton Kotton Grammer OMG Machines SCAM Don Tigert granger whitelaw Richard Cosentino John Schmutzer Exotic Images LLC CMC Choppers Jim Haszinger COSENTINO N Ripoff Reports Complaints Reviews Scams Lawsuits and Frauds Reported Jim Haszinger KaonSoftwares Kaon Softwares Emergency Vehicle Lights Light bars Sirens LED Outfitters Lise Winicki LuxSauna Auto Connection Manassas Complaint Review: Poggi Michael Michael Poggi Poggi’s Animal House Complaint Review: Michael Poggi Poggisanimalhouse 9547089441 Complaint Review: Mountain Business Center LLC Rebecca Bextel SwiftUnlocks Elite Client Services Complaint Review: Naushad Noordin Velani Complaint Review: Trey Knight Trey Knight trey night Haskel Grant Knight Haskel G Knight FHTM LIMU Danger Trey Knight Uses fake social security numbers Trey Knight Atlantic Beach, Florida Trey Knight is a Scammer LA Laser Center Gerardo Cornejo Velez 多伦多中医吴冠英 Conslines.com

  4. Richard Ellenbogen : 2017年05月20日12:47:23  -46楼 @回复 回复

    Executive pleading poverty after $44M judgment Symons and family ordered to pay $34.2 million in fraud case Alan Symons In the United States Court of Appeals Arie Abekasis Diditan Financial ARIE ABEKASIS DIDITAN FINANCIAL DIDITAN GROUP ARIE ABEKASIS OF DIDITAN Richard Ellenbogen – Beverly Hills Body Richard Ellenbogen – Beverly Hills Body – Babak Dadvand Beverly Hills Body Plastic Surgeons – Richard Ellenbogen MD Richard Ellenbogen And Babak Dadvand Plastic Surgeons Of Beverly Hills Richard Ellenbogen Plastic Surgeon And Babak Dadvand Richard Ellenbogen Md Plastic Surgery Rhinoplasty Nose Job Richard Ellenbogen Beverly Hills Body Cosmetic Plastic And Reconstructive Surgeon Richard Ellenbogen Plastic Surgeon – Babak Dadvand Richard Ellenbogen Beverly Hills Body And Babak Dadvand Richard Ellenbogen – Beverly Hills Body BRIAN WILLIS Brockstar DAN ANTON: BACKLINKSVAULT and BrandlinksRank dan anton Kotton Grammer OMG Machines SCAM Don Tigert granger whitelaw Richard Cosentino John Schmutzer Exotic Images LLC CMC Choppers Jim Haszinger COSENTINO N Ripoff Reports Complaints Reviews Scams Lawsuits and Frauds Reported Jim Haszinger KaonSoftwares Kaon Softwares Emergency Vehicle Lights Light bars Sirens LED Outfitters Lise Winicki LuxSauna Auto Connection Manassas Complaint Review: Poggi Michael Michael Poggi Poggi’s Animal House Complaint Review: Michael Poggi Poggisanimalhouse 9547089441 Complaint Review: Mountain Business Center LLC Rebecca Bextel SwiftUnlocks Elite Client Services Complaint Review: Naushad Noordin Velani Complaint Review: Trey Knight Trey Knight trey night Haskel Grant Knight Haskel G Knight FHTM LIMU Danger Trey Knight Uses fake social security numbers Trey Knight Atlantic Beach, Florida Trey Knight is a Scammer LA Laser Center Gerardo Cornejo Velez 多伦多中医吴冠英 Conslines.com

  5. CMC Choppers : 2017年05月20日12:47:38  -45楼 @回复 回复

    Executive pleading poverty after $44M judgment Symons and family ordered to pay $34.2 million in fraud case Alan Symons In the United States Court of Appeals Arie Abekasis Diditan Financial ARIE ABEKASIS DIDITAN FINANCIAL DIDITAN GROUP ARIE ABEKASIS OF DIDITAN Richard Ellenbogen – Beverly Hills Body Richard Ellenbogen – Beverly Hills Body – Babak Dadvand Beverly Hills Body Plastic Surgeons – Richard Ellenbogen MD Richard Ellenbogen And Babak Dadvand Plastic Surgeons Of Beverly Hills Richard Ellenbogen Plastic Surgeon And Babak Dadvand Richard Ellenbogen Md Plastic Surgery Rhinoplasty Nose Job Richard Ellenbogen Beverly Hills Body Cosmetic Plastic And Reconstructive Surgeon Richard Ellenbogen Plastic Surgeon – Babak Dadvand Richard Ellenbogen Beverly Hills Body And Babak Dadvand Richard Ellenbogen – Beverly Hills Body BRIAN WILLIS Brockstar DAN ANTON: BACKLINKSVAULT and BrandlinksRank dan anton Kotton Grammer OMG Machines SCAM Don Tigert granger whitelaw Richard Cosentino John Schmutzer Exotic Images LLC CMC Choppers Jim Haszinger COSENTINO N Ripoff Reports Complaints Reviews Scams Lawsuits and Frauds Reported Jim Haszinger KaonSoftwares Kaon Softwares Emergency Vehicle Lights Light bars Sirens LED Outfitters Lise Winicki LuxSauna Auto Connection Manassas Complaint Review: Poggi Michael Michael Poggi Poggi’s Animal House Complaint Review: Michael Poggi Poggisanimalhouse 9547089441 Complaint Review: Mountain Business Center LLC Rebecca Bextel SwiftUnlocks Elite Client Services Complaint Review: Naushad Noordin Velani Complaint Review: Trey Knight Trey Knight trey night Haskel Grant Knight Haskel G Knight FHTM LIMU Danger Trey Knight Uses fake social security numbers Trey Knight Atlantic Beach, Florida Trey Knight is a Scammer LA Laser Center Gerardo Cornejo Velez 多伦多中医吴冠英 Conslines.com

  6. ARIE ABEKASIS DIDITAN FINANCIAL : 2017年05月20日12:47:53  -44楼 @回复 回复

    Executive pleading poverty after $44M judgment Symons and family ordered to pay $34.2 million in fraud case Alan Symons In the United States Court of Appeals Arie Abekasis Diditan Financial ARIE ABEKASIS DIDITAN FINANCIAL DIDITAN GROUP ARIE ABEKASIS OF DIDITAN Richard Ellenbogen – Beverly Hills Body Richard Ellenbogen – Beverly Hills Body – Babak Dadvand Beverly Hills Body Plastic Surgeons – Richard Ellenbogen MD Richard Ellenbogen And Babak Dadvand Plastic Surgeons Of Beverly Hills Richard Ellenbogen Plastic Surgeon And Babak Dadvand Richard Ellenbogen Md Plastic Surgery Rhinoplasty Nose Job Richard Ellenbogen Beverly Hills Body Cosmetic Plastic And Reconstructive Surgeon Richard Ellenbogen Plastic Surgeon – Babak Dadvand Richard Ellenbogen Beverly Hills Body And Babak Dadvand Richard Ellenbogen – Beverly Hills Body BRIAN WILLIS Brockstar DAN ANTON: BACKLINKSVAULT and BrandlinksRank dan anton Kotton Grammer OMG Machines SCAM Don Tigert granger whitelaw Richard Cosentino John Schmutzer Exotic Images LLC CMC Choppers Jim Haszinger COSENTINO N Ripoff Reports Complaints Reviews Scams Lawsuits and Frauds Reported Jim Haszinger KaonSoftwares Kaon Softwares Emergency Vehicle Lights Light bars Sirens LED Outfitters Lise Winicki LuxSauna Auto Connection Manassas Complaint Review: Poggi Michael Michael Poggi Poggi’s Animal House Complaint Review: Michael Poggi Poggisanimalhouse 9547089441 Complaint Review: Mountain Business Center LLC Rebecca Bextel SwiftUnlocks Elite Client Services Complaint Review: Naushad Noordin Velani Complaint Review: Trey Knight Trey Knight trey night Haskel Grant Knight Haskel G Knight FHTM LIMU Danger Trey Knight Uses fake social security numbers Trey Knight Atlantic Beach, Florida Trey Knight is a Scammer LA Laser Center Gerardo Cornejo Velez 多伦多中医吴冠英 Conslines.com

给我留言

留言无头像?


×