文件测试操作符 (四)

2014-11-24 11:22:28 · 作者: · 浏览: 14
echo "String \"string2\" is not null."
else
echo "String \"string2\" is null."
fi

if [ $string3 ];then
echo "String \"string3\" is not null."
else
echo "String \"string3\" is null."
fi

string4=initialize
if [ $string4 ];then
echo "String \"string4\" is not null."
else
echo "String \"string4\" is null."
fi

string5="a = b"
if [ $string5 ];then
echo "String \"string5\" is not null."
else
echo "String \"string5\" is null."
fi

string6="a = b"
if [ "$string6" ];then
echo "String \"string6\" is not null."
else
echo "String \"string6\" is null."
fi

exit 0

#!/bin/bash

# -n
if [ -n $string1 ];then
echo "String \"string1\" is not null."
else
echo "String \"string1\" is null."
fi

#This is the best way
if [ -n "$string2" ];then
echo "String \"string2\" is not null."
else
echo "String \"string2\" is null."
fi

if [ $string3 ];then
echo "String \"string3\" is not null."
else
echo "String \"string3\" is null."
fi

string4=initialize
if [ $string4 ];then
echo "String \"string4\" is not null."
else
echo "String \"string4\" is null."
fi

string5="a = b"
if [ $string5 ];then
echo "String \"string5\" is not null."
else
echo "String \"string5\" is null."
fi

string6="a = b"
if [ "$string6" ];then
echo "String \"string6\" is not null."
else
echo "String \"string6\" is null."
fi

exit 0结果:


[cpp] view plaincopyprint
root@ubuntu:~/resource/shell-study/0506-2013# ./test1.sh
String "string1" is not null.
String "string2" is null.
String "string3" is null.
String "string4" is not null.
String "string5" is null.
String "string6" is not null.

root@ubuntu:~/resource/shell-study/0506-2013# ./test1.sh
String "string1" is not null.
String "string2" is null.
String "string3" is null.
String "string4" is not null.
String "string5" is null.
String "string6" is not null.
分析:

string1和string2同是未定义的string,为什么string1判断是非null,而string2判断就是null,对string1的判断明显是错误的,对string的操作加上“”是很有必要的,sting5和string6的比较同样说明了这一点

继续看一个实例:


[cpp] view plaincopyprint
#!/bin/bash

NOARGS=65
NOTFOUND=66
NOTTXT=67

if [ $# -eq 0 ];then
echo "Usage: `basename $0` no filename!" >&2
exit $NOARGS
fi

FILENAME=$1

if [ ! -f "$FILENAME" ];then
echo "File $FILENAME not found!" >&2
exit $NOTFOUND
fi

if [ ${FILENAME##*.} != "txt" ];then
echo "File $1 is not a txt file!" >&2
exit $NOTTXT
fi

cat $1 | more
exit 0

#!/bin/bash

NOARGS=65
NOTFOUND=66
NOTTXT=67

if [ $# -eq 0 ];then
echo "Usage: `basename $0` no filename!" >&2
exit $NOARGS
fi

FILENAME=$1

if [ ! -f "$FILENAME" ];then
echo "File $FILENAME not found!" >&2
exit $NOTFOUND
fi

if [ ${FILENAME##*.} != "txt" ];then
echo "File $1 is not a txt file!" >&2
exit $NOTTXT
fi

cat $1 | more
exit 0
上面几个判断条件使用方法是重点关注的地方:

1.判断参数个数为0退出

2.先判断FILENAME是否为文件,再取反,即不是文件或找不到文件退出

3.判断文件后缀名是不是“.txt”,不是则退出

结果:


[cpp] view plaincopyprint
root@ubuntu:~/resource/shell-study/0506-2013# ls
file.txt file.zip test1.sh test2.sh
root@ubuntu:~/resource/shell-study/0506-2013# ./test2.sh
Usage: test2.sh no filename!
root@ubuntu:~/resource/shell-study/0506-2013# ./test2.sh file
File file not found!
root@ubuntu:~/resource/shell-study/0506-2013# ./test2.sh file.zip
File file.zip is not a txt file!
root@ubuntu:~/resource/shell-study/0506-2013# ./test2.sh file.txt
total 7476
-rwxr-xr-x 1 root root 818232 2010-04-18 18:51 bash
-rwxr-xr-x 1 root root 30200 2010-02-08 02:54 bunzip2
-rwxr-xr-x 1 root roo