文字列が「node」で始まるかどうか(例:「node001」)を確認したいと思います。
if [ $HOST == node* ]
then
echo yes
fi
どうすれば正しく実行できますか?
HOST
さらに、式を組み合わせて、「user1」か「node」で始まるかをチェックする必要があります。
if [ [[ $HOST == user1 ]] -o [[ $HOST == node* ]] ];
then
echo yes
fi
> > > -bash: [: too many arguments
どうすれば正しく実行できますか?
ベストアンサー1
このスニペットは高度な Bash スクリプト ガイド言う:
# The == comparison operator behaves differently within a double-brackets
# test than within single brackets.
[[ $a == z* ]] # True if $a starts with a "z" (wildcard matching).
[[ $a == "z*" ]] # True if $a is equal to z* (literal matching).
つまり、ほぼ正解です。必要なのは単一の括弧ではなく、二重の括弧でした。
2 番目の質問に関しては、次のように記述できます。
HOST=user1
if [[ $HOST == user1 ]] || [[ $HOST == node* ]] ;
then
echo yes1
fi
HOST=node001
if [[ $HOST == user1 ]] || [[ $HOST == node* ]] ;
then
echo yes2
fi
それは反響するだろう
yes1
yes2
Bash のif
構文は慣れるのが難しいです (IMO)。