shell中test命令

来源:互联网 发布:mac用什么炒股软件 编辑:程序博客网 时间:2024/05/18 00:11

http://blog.csdn.net/changerjjlee/article/details/52163900


test比较两个数值:

test命令可以进行对两个值的比较,如果比较成功则返回值为0,否则为非0
常用的类型:
整数比较
字符串比较
逻辑比较(与、或、非)
文件比较
test的命令语法
test values1 -option values2
如果比较成功则$?为0,否则返回非0,此处用于判断操作
变量比较操作符表
options
命令符号
大于
-gt
等于
-eq
小于
-lt
不等于
-ne
大于等于
-ge
小于等于
-le
测试变量值是否为空
-z
测试字符串是否为非空
-n
判断两字符串是否相等
=
判断两字符串是否为空
!=

Shell实例1:
[root@ChangerLee 运算比较符]#cat test_v1.sh
#!/bin/bash

i=100
j=200
k=300
f=200

echo -e "i=$i\tj=$j\tk=$k"

test $j -gt $i
if [ $? -eq 0 ]
then
echo "j>i"
fi

test $k -ge $j
if [ $? -eq 0 ]
then
echo "k>=j"
fi

test $j -eq $f
if [ $j -eq $f ]
then
echo "j=f"
fi

[root@ChangerLee 运算比较符]#sh test_v1.sh
i=100 j=200 k=300
j>i
k>=j
j=f
Shell实例2:
变量若为0;$0则为0
[root@ChangerLee 运算比较符]#[ -z $name ];echo $?
0
[root@ChangerLee 运算比较符]#name=Jhon
[root@ChangerLee 运算比较符]#[ -z $name ];echo $?
1
Shell实例3:
变量若为非零,$?则为0
[root@ChangerLee 运算比较符]#[ -n "$sex" ];echo $?
1
[root@ChangerLee 运算比较符]#sex=man
[root@ChangerLee 运算比较符]#[ -n "$sex" ];echo $?
0
Shell实例4:
[root@ChangerLee 运算比较符]#[ "qwer" = "asdf" ];echo $?
1
[root@ChangerLee 运算比较符]#[ "qwer" != "asdf" ];echo $?
0

逻辑操作符:
Shell实例5:
[root@ChangerLee 运算比较符]# cat test_logic_v1.sh
#!/bin/bash
#test逻辑比较方法实例

basepath=${pwd}
filename=$0

echo "$basepath目录下的$filename与或非运算实例"
#与运算
test $# -eq 1 -a $1 -eq 100
if [ $? -eq 0 ]
then
echo "参数个数为1并且第一个参数为100"
else
echo "不满足参数个数不为1或者第一个参数不为100"
fi
#或运算
test $# -ge 1 -o $1 -eq 100
if [ $? -eq 0 ]
then
echo "参数个数大于1,或者第一个参数为100"
else
echo "不满足参数个数大于1并且第一个参数为100"
fi
[root@ChangerLee 运算比较符]# sh test_logic_v1.sh 100 200
目录下的test_logic_v1.sh与或非运算实例
不满足参数个数不为1或者第一个参数不为100
参数个数大于1,或者第一个参数为100
文件操作符:
文件测试命令表:
命令
功能
d
目标是否存在,并且是一个目录
f
目标文件是否存在,并且是一个普通文件
L
目标是否存在,并且是一个目标文件
b
目标文件是否存在,并且是一个块设备文件
c
目标文件是否存在,并且是一个字符设备文件
e
指定文件或目录是否存在

文件权限测试命令表
命令
功能
w
判断指定文件是否存在,并且拥有可写入权限
r
判断指定文件是否存在,并且拥有可读取权限
x
判断目标文件是否存在,并且具备可执行权限
u
判断目标文件是否具有SUID权限
常规文件:-rw-r--r--. 1 root root 0 Aug 6 07:46 test_file.sh
目录文件:drwxrwxrwx. 2 root root 4096 Aug 6 07:46 file_test
字符设备或者块设备:crw--w----. 1 root tty 4, 1 Jul 31 21:22 /dev/tty1
套接字接口文件:srw-rw-rw-. 1 root root 0 Jul 31 21:22 /run/rpcbind.sock
链接文件lrwxrwxrwx. 1 root root 13 Aug 6 08:02 test.sh -> test_file1.sh

Shell实例6:
[root@ChangerLee 运算比较符]# cat test_file.sh
#!/bin/bash
#test文件比较方法
basepath=${pwd}
filename=$0
filename2="./test_v1.sh"
dir_name="/etc/httpd"

test -f $filename
if [ $? -eq 0 ]
then
echo "$filename 存在且为常规文件"
else
echo "$filename 不存在或者不为常规文件"
fi

test -d $dir_name
if [ $? -eq 0 ]
then
echo "$dir_name 存在且为目录"
else
echo "$dir_name 不存在或者不为目录"
fi

test -x $filename2
if [ $? -eq 0 ]
then
echo "$filename2 存在且为可执行文件"
else
echo "$filename2 不存在或者不可执行"
fi

[root@ChangerLee 运算比较符]# sh test_file.sh
test_file.sh 存在且为常规文件
/etc/httpd 存在且为目录
./test_v1.sh 不存在或者不可执行
[root@ChangerLee 运算比较符]# chmod 777 test_v1.sh
[root@ChangerLee 运算比较符]# sh test_file.sh
test_file.sh 存在且为常规文件
/etc/httpd 存在且为目录
./test_v1.sh 存在且为可执行文件
Shell实例7:
[root@ChangerLee 运算比较符]# cat testfiledir_v1.sh
#!/bin/bash
#testfiledir_v1
#example of opration
test $# -eq 1
if [ $? -eq 0 ]
then
test -f $1
if [ $? -eq 0 ]
then
test -x $1
if [ $? -ne 0 ]
then
chmod 777 $1
fi
else
echo "this an dir"
fi
else
echo "error:just one argument"
fi
[root@ChangerLee 运算比较符]# sh testfiledir_v1.sh file
[root@ChangerLee 运算比较符]# ll file
-rwxrwxrwx. 1 root root 0 Aug 6 08:46 file
原创粉丝点击