I’m currently writing a bash script for executing test suites. Besides passing the suites directly to this script, like
./bash-specs test.suite
it should also be able to execute all scripts in a given directory if no suite is passed to it, like so
./bash-specs # executes all tests in the directory, namely test.suite
This is implemented like this
(($# == 0)) && set -- *.suite
So, if no suite is passed, all the files ending on .suite are executed. This works fine but fails if the directory contains no such files.
That means I will also need a check to test if there actually are files with that ending.
How would I do this in bash?
I thought a test like
[[ -f *.suite ]]
should work but it seems to fail when there are more than one file in the directory.
The reason
-fis failing is because-fonly takes a single parameter. When you do[[ -f *.suite ]], it expands to:… which is not valid.
Instead, do this:
nullglobis a shell option that makes wildcard patterns that aren’t found expand to nothing, rather than expanding to the wildcard pattern itself. Once$FILESis set to either a list of files or nothing, we can use-zto test for emptiness, and display the appropriate error message.