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?
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.
“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”.
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 [[ ?.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With