I write the following shell script.
#! /bin/sh
foo=asdfqwer/asdfxv
if [ $foo = */* ]
then
echo bad
else
echo good
fi
in test command, we could compare string and pattern like this,
[ string = pattern ]
[ string == pattern ]
however, the above script always print “good” in the terminal and also has error as below:
[ : asdfqwer/asdfxv : unexpected operator
Can someone tells me why and how to compare a stirng and a pattern in shell scripting?
The
testcommand (or[command), does not do globbed comparison. Instead, the shell is expanding the*/*to match files in your directory, and substituting them in to that command. Presumably, one of the filenames gets parsed as an operator to the[command, and is invalid.The best way to compare against globs is
case: