Reputation:
I don't understand why this if
statement is giving an error saying:
Using too many arguments
if [ $file == 'Metro[0-9]*' ] -o [ $file == 'Store[0-9]*' ]
echo "$file"
I literally used every combination of parentheses thinking that there might be a mistake but nothing changed. The interesting thing is, condition works if I just use one of the conditions and erase the other. So I assume there is something wrong with using the -o
or parentheses...
Upvotes: 3
Views: 12781
Reputation: 189377
There is no shell operator -o
, you seem to be looking for
if [[ $file == 'Metro'[0-9]* || $file == 'Store'[0-9]* ]]; then
echo "$file"
fi
which is of course somewhat more succinctly and portably written
case $file in 'Metro'[0-9]* | 'Store'[0-9]*) echo "$file";; esac
Notice the multiple changes in quoting: wildcards mustn't be inside quotes if you want them expanded, and the file name should generally be inside double quotes (though it's not necessary after [[
or case
).
Notice also the change to [[
which is not a POSIX sh
command -- the first alternative above is specific to Bash / Ksh / other "modern" extended shells. The POSIX [
(aka test
) doesn't have a simple way to check if a file matches an arbitrary wildcard expression. Indicentally, Bash also supports if [ condition1 -o condition2 ]
but that isn't portable, either, so in a way the worst of two worlds.
Upvotes: 7
Reputation: 10138
if
condition is missing then
and fi
.-o
operator does not exist, but you can use ||
to represent OR.=~
operator to compare a string against a regex.So you can write your condition like this:
if [[ "$file" =~ (Metro|Store)[0-9]* ]] ; then
echo "$file"
fi
Upvotes: 2