Bash - IF と && 構文?

Bash - IF と && 構文?

IF ステートメントと && ステートメントについて少し助けていただきたいです。うまくいかず、試しているものも何もうまくいきません。誰かこの問題に気付いている人がいるかもしれません:

if [[ $thisvariable="yes" && grep 'this string' ./"$thisfile".txt ]]; then

これは機能しません。問題があるとわかる人はいますか?

答え1

これを試して:

if [[ "$thisvariable" = "yes" ]] && grep -q 'this string' ./"${thisfile}".txt; then

答え2

最後の投稿を今見たばかりなので、試していません。最終的にこれで動作するようになりました:

if [ $thisvariable == yes ] && [ "`grep 'this string' ./"thisfile"`" ]; then

答え3

この[構造は複数の条件を取ることはできません。 試みていることは複数の方法で記述できます:

if [ "$thisvariable" = "yes" ] && grep 'this string' ./"thisfile" > /dev/null
then 
    echo yes; 
fi

あるいはもっと簡単に言えば

[ "$thisvariable" = "yes" ] && grep 'this string' ./"thisfile" > /dev/null && echo yes

答え4

から:http://tldp.org/LDP/abs/html/comparison-ops.html#SCOMPARISON1

if [ "$a" = "$b" ]

注意!「=」を囲む空白に注意してください。

if [ "$a"="$b" ] is not equivalent to the above.

関連情報