Bash conditionals: how to “and” expressions? (if [ ! -z $VAR && -e $VAR ])

前端 未结 5 1655
故里飘歌
故里飘歌 2021-01-29 22:04

I guess I\'m not clear on how to do \"and\" tests. I wanted to make sure an argument existed which was working well with [ -e $VAR ], but it turns out that was also

相关标签:
5条回答
  • 2021-01-29 22:34

    From the bash manpage:

    [[ expression ]] - return a status of 0 or 1 depending on the evaluation of the conditional expression expression.

    And, for expressions, one of the options is:

    expression1 && expression2 - true if both expression1 and expression2 are true.

    So you can and them together as follows (-n is the opposite of -z so we can get rid of the !):

    if [[ -n "$var" && -e "$var" ]] ; then
        echo "'$var' is non-empty and the file exists"
    fi
    

    However, I don't think it's needed in this case, -e xyzzy is true if the xyzzy file exists and can quite easily handle empty strings. If that's what you want then you don't actually need the -z non-empty check:

    pax> VAR=xyzzy
    pax> if [[ -e $VAR ]] ; then echo yes ; fi
    pax> VAR=/tmp
    pax> if [[ -e $VAR ]] ; then echo yes ; fi
    yes
    

    In other words, just use:

    if [[ -e "$var" ]] ; then
        echo "'$var' exists"
    fi
    
    0 讨论(0)
  • 2021-01-29 22:44

    Simply quote your variable:

    [ -e "$VAR" ]
    

    This evaluates to [ -e "" ] if $VAR is empty.

    Your version does not work because it evaluates to [ -e ]. Now in this case, bash simply checks if the single argument (-e) is a non-empty string.

    From the manpage:

    test and [ evaluate conditional expressions using a set of rules based on the number of arguments. ...

    1 argument

    The expression is true if and only if the argument is not null.

    (Also, this solution has the additional benefit of working with filenames containing spaces)

    0 讨论(0)
  • 2021-01-29 22:47
    if [ ! -z "$var" ] && [ -e "$var" ]; then
          # something ...
    fi
    
    0 讨论(0)
  • 2021-01-29 22:49
    if [ -n "$var" -a -e "$var" ]; then
        do something ...
    fi
    

     

    0 讨论(0)
  • 2021-01-29 22:56

    I found an answer now. Thanks for your suggestions!

    for e in ./*.cutoff.txt; do
    if grep -q -E 'COX1|Cu-oxidase' $e
    then
        echo xyz >$e.match.txt
    else
        echo
    fi
    
    if grep -q -E 'AMO' $e
    then
        echo abc >$e.match.txt
    else
        echo
    fi; done
    

    Any comments on that? It seems inefficient to grep twice, but it works...

    0 讨论(0)
提交回复
热议问题