條件測試

條件測試

test 命令

一.測試文件

一般形式:

1.   test condition

2.   [ condition ] 

文件狀態 ,例如 test –d name 或者 [ -d name ]

-d

目錄

-s

文件長度大於 0 ,非空

-f

正規文件

-w

可寫

-L

符號鏈接

-u

文件有 suid 位設置

-r

可讀

-x

可執行

 

$ test –d name

$ $?

$ 0

0 表示成功, 1 表示返回錯誤

$? 退出最後狀態命令,用來檢查測試結果;

 

二.測試時使用的邏輯符號

-a 邏輯與

-o 邏輯或

!邏輯否

例如

$ [ -w name1 –a –w name2 ]

$ $?

$ 0

三.測試字符串

一般形式:

test “string”

test string_operator “string”

test “string” string_operator “string”

[string_operator string ]

[string string_operator string]

string_operator

=

兩個字符串相等

-z

空串

!=

兩個字符串不相等

-n

非空串

 

四.測試數值

         一般形式

         “number” mumeric_operator “number”

         [ “number” mumeric_operator “number” ]

        

mumeric_operator :

-eq

數值相等

-lt

第一個數小於第二個

-ne

數值不相等

-le

第一個數小於等於第二個

-gt

第一個數大於第二個

-ge

第一個數大於等於第二個

 

例如

$ a=130

$ b=120

$ [ “$a” –lt “$b” ]

$ echo $?

$ 1

 

可以使用邏輯操作符

如:

$ [ “990” –le “995” ] –a [ “123” –ge “124” ]

$ echo $?

$ 1

 

五. expr 命令測試執行和執行數值輸出

         一般用於整數值或者字符串

         一般形式

         expr argument operator argument

         expr 也是手工命令行計數器

         $ expr 10 + 10

         20

         減法 除法 / 乘法 /*

1.        增量技術

expr 用於增量計算

$ LOOP=0

$ LOOP=`expr $LOOP + 1`

2.        $ value=100

$ expr $value + 10 >/dev/null 2>&1

$ echo $?

0

表示是個數值

$ value=hello

$ expr $value + 10 >/dev/null 2>&1

$ echo $?

3

表示是個非數值符號

3.        模式匹配

字符串匹配操作

$ expr $calue : `/(.*/).txt`

Hdisk.txt

在shell中的應用 循環計數


#!/bin/sh
counter=0
for files in *
do
counter=`expr $counter + 1`
done
echo "There are $counter files in `pwd` we need to process"

 

 

 

 

整數比較

-eq

等於

if [ "$a" -eq "$b" ]

-ne

 不等於

if [ "$a" -ne "$b" ]

-gt

 大於

if [ "$a" -gt "$b" ]

-ge

 大於等於

if [ "$a" -ge "$b" ]

-lt

小於

if [ "$a" -lt "$b" ]

-le

小於等於

if [ "$a" -le "$b" ]

<

小於(需要雙括號)

(("$a" < "$b"))

<=

小於等於(需要雙括號)

(("$a" <= "$b"))

> 大於

(需要雙括號)

(("$a" > "$b"))

>=

大於等於(需要雙括號)

(("$a" >= "$b"))

字符串比較

=

 等於

if [ "$a" = "$b" ]

==

等於

if [ "$a" == "$b" ],與=等價

注意:==的功能在[[]]和[]中的行爲是不同的,如下:

 [[ $a == z* ]]

# 如果$a 以"z"開頭(模式匹配)那麼將爲true

 [[ $a == "z*" ]]

# 如果$a 等於z*(字符匹配),那麼結果爲true

 [ $a == z* ]

# File globbing 和word splitting 將會發生

 [ "$a" == "z*" ]

# 如果$a 等於z*(字符匹配),那麼結果爲true

!=

不等於

if [ "$a" != "$b" ]

 

<

小於,在ASCII 字母順序下.如:if [[ "$a" < "$b" ]]if [ "$a" /< "$b" ]

 

注意:在[]結構中"<"需要被轉義.

>

大於,在ASCII 字母順序下.如:if [[ "$a" > "$b" ]]if [ "$a" /> "$b" ]

 

注意:在[]結構中">"需要被轉義.

-z

字符串爲"null".就是長度爲0.

-n

字符串不爲"null"

 

 

 

For Mathematics, use following operator in Shell Script

Mathematical Operator in  Shell Script 

Meaning

Normal Arithmetical/ Mathematical Statements

But in Shell

 

 

 

For test statement with if command

For [ expr ] statement with if command

-eq

is equal to

5 == 6

if test 5 -eq 6

if [ 5 -eq 6 ]

-ne

is not equal to

5 != 6

if test 5 -ne 6

if [ 5 -ne 6 ]

-lt

is less than

5 < 6

if test 5 -lt 6

if [ 5 -lt 6 ]

-le

is less than or equal to

5 <= 6

if test 5 -le 6

if [ 5 -le 6 ]

-gt

is greater than

5 > 6

if test 5 -gt 6

if [ 5 -gt 6 ]

-ge

is greater than or equal to

5 >= 6

if test 5 -ge 6

if [ 5 -ge 6 ]

 

For string Comparisons use

Operator

Meaning

string1 = string2

string1 is equal to string2

string1 != string2

string1 is NOT equal to string2

string1

string1 is NOT NULL or not defined 

-n string1

string1 is NOT NULL and does exist

-z string1

string1 is NULL and does exist

Shell also test for file and directory types

Test

Meaning

-s file   

Non empty file

-f file   

Is File exist or normal file and not a directory 

-d dir    

Is Directory exist and not a file

-w file  

Is writeable file

-r file   

Is read-only file

-x file   

Is file is executable

Logical Operators

              Logical operators are used to combine two or more condition at a time

Operator           

Meaning

! expression

Logical NOT

expression1  -a  expression2

Logical AND

expression1  -o  expression2

Logical OR

 

 

發佈了41 篇原創文章 · 獲贊 2 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章