Bash shell:返回文件/目录上的测试值
我无法检查函数测试的返回值;男人测试对我帮助不大Bash shell:返回文件/目录上的测试值,bash,shell,Bash,Shell,我无法检查函数测试的返回值;男人测试对我帮助不大 #!/bin/bash test=$(test -d $1) if [ $test -eq 1 ] then echo "the file exists and is a directory" elif [ $test -eq 0 ] echo "file does not exist or is not a directory" else echo "error" fi $(test-d$1)将被替换为测试输出,而不
#!/bin/bash
test=$(test -d $1)
if [ $test -eq 1 ]
then
echo "the file exists and is a directory"
elif [ $test -eq 0 ]
echo "file does not exist or is not a directory"
else
echo "error"
fi
$(test-d$1)将被替换为测试输出,而不是其返回代码。如果要检查其返回代码,请使用$?
,例如
试试吧
if test -d $1
then
echo 'the file exists and is a directory'
else
echo 'the file doesn't exist or is not a directory'
fi
每次你在
test
的返回码上使用test
,上帝就会杀死一只小猫
if test -d "$1"
或
对不起,伊格纳西奥。。。可怜的小猫们:p@nour:换句话说:
[
是test
(两者都是Bash内置程序和外部程序(/usr/bin/[
和/usr/bin/test
)。这使用了bash内置,因此速度可能会快一些。虽然它的测试方式与@Steve Emmerson的解决方案大致相同。我更喜欢这种方法。不。不要这样做。只需执行“if-test-d$1;then…else…;fi”。或者,甚至“test-d$1&&…”是的,对于测试,您不会检查$,因为它只是为了返回0/1。但一般情况下,对于具有多个返回值的程序,使用$。
if test -d "$1"
if [ -d "$1" ]