linux shell测试文件状态

写脚本是,有时要判断字符串是否相等,可能还要检查文件状态或是数字测试。

Test命令用于测试字符串,文件状态和数字。

 

对文件、字符串和数字使用test命令

对数字和字符串使用expr命令

 

 expr命令测试和执行数值输出。使用最后退出状态命令$?可测知test和expr,二者均以0表示正确,1表示返回错误。

 

test一般有两种格式,即:

test condition 或 [ condition ]

使用方括号时,要注意在条件两边加上空格。

文件状态测试

-d 目录        -s 文件长度大于0、非空

-f 正规文件   -w 可写

-L 符号连接  -u 文件有suid位设置

-r 可读         -x 可执行 

 

example:

#!/bin/sh
#this is a shell command about test the file is a dir
echo "begin test the ~/input"
test -d ~/input
echo $? 

测试时使用逻辑操作符

测试文件状态是否为OK,但是有时要比较两个文件状态。shell提供三种逻辑操作完成此功能。

-a 逻辑与,操作符两边均为真,结果为真,否则为假。

-o 逻辑或,操作符两边一边为真,结果为真,否则为假。

example:

测试两个文件是否均可读

#!/bin/sh
#this is a command to test -a 
echo "begin test"
ls -l ~/test/
test -w file1 -a -w file2
echo $?
echo "end test"

result:

begin test
total 8
-rw-rw-r-- 1 hadoop hadoop 0 Feb  6 02:45 file1
-rw-rw-r-- 1 hadoop hadoop 0 Feb  6 02:45 file2
0
end test

 

example:

测试其中的一个文件是否可执行

#!/bin/sh
#this is a command to test -o
echo "test begin"
ls -l 
[ -x file1 -o -x file2 ]
echo $?
echo "test end" 

result:

test begin
total 8
-rwxrw-r-- 1 hadoop hadoop 0 Feb  6 02:45 file1
-rw-rw-r-- 1 hadoop hadoop 0 Feb  6 02:45 file2
0
test end

 

example:

测试文件file1是否可读、可写、可执行

#!/bin/sh
#this is a command to test a file "rwx"
echo "begin test"
ls -l file1
[ -r file1 -a -w file1 -a -x file1 ]
echo $?
echo "end test" 

result:

begin test
-rwxrw-r-- 1 hadoop hadoop 0 Feb  6 02:45 file1
0
end test

 

 字符串测试

字符串测试时错误捕获很重要的一部分,特别在测试用户输入或比较变量时尤为重要。

test "string"

test string_operator "string"

test "string" string_operator "string"

[ string_operator string ]

[ string string_operator string ]

这里,string_operator可为

= 两个字符串相等

!= 两个字符串不等

-z 空串

-n 非空串

 

给变量editor赋值为

[hadoop@master test]$ editor=vi
[hadoop@master test]$ echo $editor
vi
 [hadoop@master test]$ editor="vi test"
[hadoop@master test]$ echo $editor
vi test

 注意:在变量和字符串之间的等号不能有空格

 

example:

#!/bin/sh
#this is a command to test string
editor=vi
echo $editor 
echo "test begin"
echo "is the string null?"
[ -z $editor ]
echo $?
echo "is the string vi?"
[ $editor = "vi" ]
echo $?
echo $editor
tape="/dev/rmt0"
tape2="/dev/rmt1"
echo "tape:$tape"
echo "tape2:$tape2"
echo "is the tape = tape2?"
[ "$tape"="$tape2" ]
echo $?
echo "is the tape != tape2?"
[ "$tape"!="$tape2" ]
echo $?
echo "end test"

result:

vi
test begin
is the string null?
1
is the string vi?
0
vi
tape:/dev/rmt0
tape2:/dev/rmt1
is the tape = tape2?
0
is the tape != tape2?
0
end test

 

测试数值

"number"numeric_operator"number"

或者

[ "number"numeric_operator"number" ]

number_operator

-eq 数值相等

-ne 数值不相等

-gt 第一个数大于第二个数

-lt 第一个数小于第二个数

-le 第一个数小于等于第二个数

-ge 第一个数大于等于第二个数

 

example:

#!/bin/sh
#this is a command to test number
echo "test begin"
number=130
echo "is number eq 130?"
[ "$number" -eq "130" ]
echo $?
echo "is number eq 100?"
[ "$number" -eq "100" ]
echo $?
echo "is number gt 100?"
[ "$number" -gt "100" ]
echo $?
source_count=13
dest_count=15
[ "$dest_count" -gt "$source_count" ]
echo "is 15 gt 13 "
echo $?
[ "990" -le "995" ]
echo "is 990 le 995?"
echo $?
[ "990" -le "995" -a "123" -gt "33" ]
echo "is 990 le 995 and 123 gt 33?"
echo $?
echo "end test" 

result:

test begin
is number eq 130?
0
is number eq 100?
1
is number gt 100?
0
is 13 gt 15 
0
is 990 le 995?
0
is 990 le 995 and 123 gt 33?
0
end test

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值