Shell test 命令
感谢菜鸟教程:http://www.runoob.com/linux/linux-shell-test.html
Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值、字符和文件三个方面的测试。
数值测试
实例:
num1=100
num2=100
#if test $[num1] -eq $[num2]
#if test ${num1} -eq ${num2}
if test ${num1} = ${num2}
then
echo '两个数相等!'
else
echo '两个数不相等!'
fi
字符串测试
实例:
num1="ru1noob"
num2="runoob"
if test $num1 = $num2
then
echo '两个字符串相等!'
else
echo '两个字符串不相等!'
fi
输出结果:
两个字符串不相等!
文件测试
文件测试
测试当前目录下面有没有1.txt.
if test -e 1.txt
then
echo '文件1.txt已存在!'
else
echo '文件1.txt不存在!'
fi
输出结果:
文件已存在!
cd /bin
if test -e ./notFile -o -e ./bash
then
echo '有一个文件存在!'
else
echo '两个文件都不存在'
fi
输出结果:
有一个文件存在!