Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.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-测试目录是否包含以.suite结尾的文件_Bash - Fatal编程技术网

bash-测试目录是否包含以.suite结尾的文件

bash-测试目录是否包含以.suite结尾的文件,bash,Bash,我目前正在编写一个用于执行测试套件的bash脚本。除了将套件直接传递到此脚本之外,如 ./bash-specs test.suite 如果没有向它传递任何套件,它也应该能够执行给定目录中的所有脚本,就像这样 ./bash-specs # executes all tests in the directory, namely test.suite 这是这样实现的 (($# == 0)) && set -- *.suite 因此,如果没有传递任何套件,则执行以.suite结尾的

我目前正在编写一个用于执行测试套件的bash脚本。除了将套件直接传递到此脚本之外,如

./bash-specs test.suite
如果没有向它传递任何套件,它也应该能够执行给定目录中的所有脚本,就像这样

./bash-specs # executes all tests in the directory, namely test.suite
这是这样实现的

(($# == 0)) && set -- *.suite
因此,如果没有传递任何套件,则执行以.suite结尾的所有文件。这可以正常工作,但如果目录中不包含此类文件,则会失败

这意味着我还需要一个检查来测试是否确实存在以该结尾的文件。 在bash中我将如何做到这一点

我以为考试是这样的

[[ -f *.suite ]]
应该可以工作,但当目录中有多个文件时,它似乎会失败

ls -al | grep "\.suite";echo $?
如果文件存在,则显示0;如果文件不存在,则显示1


如果文件存在,则显示0;如果文件不存在,则显示1

for i in *.suite ; do
    if [ -x $i ] ; then
        echo running $i
    fi
done

我会像这样迭代每个套件文件:

for i in *.suite ; do
    if [ -x $i ] ; then
        echo running $i
    fi
done

-f
失败的原因是
-f
只接受一个参数。当您执行
[[-f*.suite]]
时,它将扩展为:

[[ -f test.suite test2.suite test3.suite ]]
。。。这是无效的

相反,请执行以下操作:

shopt -s nullglob
FILES=`echo *.suite`
if [[ -z $FILES ]]; then 
    echo "No suites found"
    exit
fi

for i in $FILES; do
    # Run your test on file $i
done

nullglob
是一个shell选项,它使未找到的通配符模式扩展为空,而不是扩展为通配符模式本身。一旦将
$FILES
设置为文件列表或无,我们就可以使用
-z
来测试是否为空,并显示相应的错误消息。

失败的原因是
-f
只接受一个参数。当您执行
[[-f*.suite]]
时,它将扩展为:

[[ -f test.suite test2.suite test3.suite ]]
。。。这是无效的

相反,请执行以下操作:

shopt -s nullglob
FILES=`echo *.suite`
if [[ -z $FILES ]]; then 
    echo "No suites found"
    exit
fi

for i in $FILES; do
    # Run your test on file $i
done
nullglob
是一个shell选项,它使未找到的通配符模式扩展为空,而不是扩展为通配符模式本身。一旦将
$FILES
设置为文件列表或无,我们就可以使用
-z
来测试是否为空,并显示相应的错误消息。

紧接着:

($#==0))&设置--*.suite

如果$1为空(带-z),则表示没有名为*.suite的文件。

紧接着:

($#==0))&设置--*.suite

如果$1为空(带-z),则表示没有名为*.suite的文件