There's the [[ var = pattern ]]
or [[ var == pattern ]]
operator of the Korn shell (also supported by a few other shells these days, but not part of the standard sh
syntax), and there's the [
command that supports the =
operator for string equality comparison.
Some [
implementations support a ==
operator as well but that's just as an alias to =
for string equality, not pattern matching. Some [
implementations like the [
builtin of yash
or zsh
support a =~
operator for regexp matching, but again, that's not available in standard sh
+ utilities syntax.
[
being just a ordinary command,
[ "$FILE" == "csharp-plugin"* ]
Is interpreted the same as:
echo "$FILE" == "csharp-plugin"* ]
Or any simple command, and "csharp-plugin"*
is expanded by the shell to the list of file names in the current directory that start with csharp-plugin
via a mechanism called globbing or filename generation or pathname expansion and pass them as separate arguments to [
.
So if the current directory contained csharp-plugin1
and csharp-plugin2
, [
would be called with [
, csharp-plugin1
, ==
, csharp-plugin1
, csharp-plugin2
and ]
as arguments, and complain about that extra csharp-plugin2
argument which it can't make sense of.
Pattern matching in sh
is done with the case
construct.
oldPlugin="/old/plugins/"
cd -- "$oldPlugin" || exit
for FILE in *; do
case "$FILE" in
(csharp-plugin* | flex-plugin* | go-plugin* |...)
rm -rf -- "$extensionPlugin"/"$FILE";;
esac
done
If you wanted to use a if
, construct, you could define a helper function such as:
match()
case $1 in
($2) true;;
(*) false;;
esac
And use it as:
if match "$FILE" 'csharp-plugin*' || match...; then
...
fi
In the Korn shell, or with the bash shell and after shopt -s extglob
(not needed in recent versions of bash
) or with zsh and after set -o kshglob
), you could also do:
if [[ $FILE = @(csharp|flex|go|...)-plugin* ]]; then...
In zsh
, without kshglob
, alternation is done simply with:
if [[ $FILE = (csharp|flex|go|...)-plugin* ]]; then...
In sh
, you could simplify a bit with:
oldPlugin="/old/plugins/"
cd -- "$oldPlugin" || exit
for FILE in *-plugin*; do
case "${FILE%%-plugin*}" in
(csharp | flex | go |...)
rm -rf -- "$extensionPlugin"/"$FILE";;
esac
done
*
would expand to all matching file and breaktest
syntax). – Archemar Aug 01 '22 at 07:34can you please help
– Samurai Aug 01 '22 at 07:47\
(but beware of quotes). You can also add linefeeds after a||
or&&
– cas Aug 01 '22 at 12:26[[ "$var" == foo* ]]
is different from[ "$var" == foo* ]
. The former does pattern matching of the contents of$var
againstfoo*
. The latter does filename generation globbing offoo*
and then an equality test if that results in only one word, and an error if it results in more than one word (from more than one matching file). – ilkkachu Aug 01 '22 at 12:46