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.

相關內容