if test 用法
(2013-01-13 21:55:07)
标签:
bashshelliftest用法it |
分类: Bash/shell |
1)判断表达式
if test
if test
!表达式为假
test
表达式1 –a 表达式2
test
表达式1 –o 表达式2
2)判断字符串
test –n
字符串
test –z
字符串
test
字符串1=字符串2
test
字符串1!=字符串2
3)判断整数
test
整数1 –eq 整数2
test
整数1 –ge 整数2
test
整数1 –gt 整数2
test
整数1 –le 整数2
test
整数1 –lt 整数2
test
整数1 –ne 整数2
4)判断文件
test
test
test
test –b
File
test –c
File
test –d
File
test –e
File
test –f
File
test –g
File
test –G
File
test –h
File
test –k
File
test –b
File
test –L
File
test –o
File
test –p
File
test –r
File
test –s
File
有关测试和多条件测试,可以看一个例子:
这个shell的作用是判断指定目录下,每一个文件是不是shell脚本文件,判断的依据是这个文件的开始部分是不是以“#!”开始的。
-
#!/bin/bash
-
-
TESTCHARS=2
-
head="#!"
-
PATHNAME=$(pwd)
-
-
if
[ "$#" -eq 1 -a -d "$1" ]
-
then
-
PATHNAME=$1
-
fi
-
-
for
file in $(ls $PATHNAME) -
do
-
if test -f $file ; then
-
headchar=`head -c$TESTCHARS $file`
-
if test $headchar="$head"
-
then
-
echo "file $file is a script" -
else
-
echo "file $file is not a script" -
fi
-
else
-
echo "file $file is not a script" -
fi
-
done
- exit 0