Jeanluca Scaljeri
Jeanluca Scaljeri

Reputation: 29129

bash: if statement with multiple checks

In my bash script I need to check if the first CLI is defined and the second one is an existing file

Here is what I have:

if [!$2] && [! -f $1 ]; then
    ....
fi

So $2 should exist (string) and $1 should be the existing file on the filesystem! Any suggestions ?

Upvotes: 0

Views: 163

Answers (1)

fedorqui
fedorqui

Reputation: 289795

If by suggestions you mean what do I need to make it work, then what you need to do is to add spaces around brackets. Also it is good to quote the variables:

if [ -n "$2" ] && [ ! -f "$1" ]; then
   ...
fi

From man test:

-n STRING

the length of STRING is nonzero

Upvotes: 3

Related Questions