Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash shell:返回文件/目录上的测试值_Bash_Shell - Fatal编程技术网

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" ]