In a higher-level programming language I am working on writing a wildcardMatch(input, pattern)
function that I want to work exactly like the glob matching in Unix.
To do this I have been using property-based testing to generate random input and test it against my implementation and the Unix implementation and then comparing that their return values are the same.
My problem is that I don't trust my simple Unix helper script:
#!/bin/bash
## Created for development/testing
##
## Example Usage:
## ./wildcard_test.sh "foobar" "fooba*"
string=$1
pattern=$2
if [[ $string == $pattern ]]; then
echo 0
exit 0
else
echo 1
exit 1
fi
Everything was working fine until I learned that the expansion would happen before the script actually runs:
./wildcart_test.sh "foo" "???"
In that example, the question marks will be expanded to actually match other files in that directory causing the ==
comparison to fail. In this case it was expanding to lib
, for example.
Is there a better way to test glob matching?
bash
and in exactly the way you show, then the???
pattern would not be expanded to any existing filenames in the current directory. Can you show some sort of transcript of this actually happening?echo $pattern
before the comparison and there it was outputtinglib
, but it seems that the comparison actually did succeed.$pattern
would be causing globbing to occur. Do look at Stéphane Chazelas' answer for a more portable test though.