Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the && operator in an if statement

I have three variables:

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

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

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

When I write this code it gives error. What is the right way?

like image 789
Ziyaddin Sadigov Avatar asked May 06 '13 09:05

Ziyaddin Sadigov


People also ask

Where do we use the article the?

English has two articles: the and a/an. The is used to refer to specific or particular nouns; a/an is used to modify non-specific or non-particular nouns. We call the the definite article and a/an the indefinite article. For example, if I say, "Let's read the book," I mean a specific book.

Where we have to use the and the?

“The” is typically used in accompaniment with any noun with a specific meaning, or a noun referring to a single thing. The important distinction is between countable and non-countable nouns: if the noun is something that can't be counted or something singular, then use “the”, if it can be counted, then us “a” or “an”.


1 Answers

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 [[ ?.

like image 62
fedorqui 'SO stop harming' Avatar answered Oct 12 '22 00:10

fedorqui 'SO stop harming'