Using the && operator in an if statement

前端 未结 1 630
日久生厌
日久生厌 2020-11-27 03:34

I have three variables:

VAR1=\"file1\"
VAR2=\"file2\"
VAR3=\"file3\"

How to use and (&&) operator in if statement like

相关标签:
1条回答
  • 2020-11-27 04:16

    So to make your expression work, changing && for -a will do the trick.

    It is correct like this:

     if [ -f $VAR1 ] && [ -f $VAR2 ] && [ -f $VAR3 ]
     then  ....
    

    or like

     if [[ -f $VAR1 && -f $VAR2 && -f $VAR3 ]]
     then  ....
    

    or even

     if [ -f $VAR1 -a -f $VAR2 -a -f $VAR3 ]
     then  ....
    

    You can find further details in this question bash : Multiple Unary operators in if statement and some references given there like What is the difference between test, [ and [[ ?.

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