Bash – IF- und &&-Syntax?

Bash – IF- und &&-Syntax?

Ich könnte ein wenig Hilfe bei meiner IF- und &&-Anweisung gebrauchen. Sie funktioniert nicht und nichts, was ich versuche, funktioniert auch. Vielleicht erkennt jemand das Problem:

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

Das funktioniert einfach nicht. Sieht jemand ein Problem?

Antwort1

Versuche dies:

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

Antwort2

Ich sehe gerade erst den letzten Beitrag, also habe ich das nicht versucht. Damit habe ich es endlich zum Laufen gebracht:

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

Antwort3

Die [Konstruktion kann nicht mehrere Bedingungen annehmen. Was Sie versuchen, kann auf mehrere Arten geschrieben werden:

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

oder einfacher

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

Antwort4

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

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

Achtung! Beachten Sie das Leerzeichen um das „=“.

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

verwandte Informationen