以查找文件为例:同级目录下存在find 不存在find1
#!/bin/bash
if test -e find;then
echo "ok"
else
echo "no"
fi
if [ -e find ];then
echo "ok"
else
echo "no"
fi
ok
ok
#!/bin/bash
if test -e find1;then
echo "ok"
else
echo "no"
fi
if [ -e find1 ];then
echo "ok"
else
echo "no"
fi
no
no
#!/bin/bash
test -e find
echo $?
test -e find1
echo $?
[ -e find ]
echo $?
[ -e find1 ]
echo $?
0
1
0
1
结论
if 是通过其后的命令执行完成的结果
$? = 0
$? = 1
从而判断条件的真/假
#!/bin/bash
test -e find
echo $?
test -e find1
echo $?
[ -e find ]
echo $?
[ -e find1 ]
echo "干扰"
echo $?
0
1
0
干扰
0
|