0

So I'm writing a script that has to check if a file exists or not before executing. I have the following code:

if [[ \( -d "DIR2" \) != true ]];
then
    echo "nonexistent dir"
    exit 1
elif [[ \( -d "DIR1" || -f "DIR1" \) != true ]];
then
    echo "nonexistent dir or file"
    exit 1
fi

I seem to be doing something wrong with the brackets since I get the following:

./syncdir.sh: line 11: conditional binary operator expected
./syncdir.sh: line 11: syntax error near `-d'
./syncdir.sh: line 11: `if [[ \( -d "DIR2" \) != true ]];'

I find it odd that bash is expecting a binary operator isnt '!=' one?
Can someone please tell me what I am doing wrong and why?


I really get confused with the brackets, I still haven't got the hang of it.

6
  • For conditionals you start here. However, what you are doing is bad practice because your code (even when working) is a perfect race condition. You better simply access the folder and handle the error if the folder does not exist. Commented Oct 9, 2015 at 23:33
  • How does it create a race condition? Commented Oct 9, 2015 at 23:48
  • 1
    @MiguelM The race condition is this: your test succeeds because the directory exists, then some other process deletes the directory, and then your process tries to use the directory that no longer exists. Commented Oct 10, 2015 at 0:02
  • How should I handle this correctly? Thanks for the previous explanation BTW Commented Oct 10, 2015 at 0:15
  • @MiguelM Like I said, simply access the folder. If the folder does not exist, you'll get an error which you can handle. This would be ok for simple situations. More sophisticated would be to use locks using flock. Commented Oct 10, 2015 at 1:27

1 Answer 1

2

Your condition syntax is totally wrong. It should be:

if [[ -d "DIR2" ]];

and

if [[ ! ( -d "DIR1" ||  -f "DIR1" ) ]];

If you have parentheses inside a conditional expression, you don't need to escape them (that's only needed if you're using the [ command). And you don't need to compare with true or false.

Sign up to request clarification or add additional context in comments.

5 Comments

So I can use parentheses inside [[]] ? But what was causing the error in the first place?
The error was because you escaped the parentheses. So it treated them as literal strings, not part of the expression syntax.
Even if I take the () it gives me an error and the solution you gave me also doesn't work :/ but I think I can get it to work. My objective was to check if DIR1 was a file or directory and if DIR2 was only a directory.
@Barmar - is there any specification when to use [ and [[ for conditional expressions ? please advise.
[[ is a bash and zsh extension, [ is a POSIX standard. But if you don't need to be portable to all POSIX shells, [[ is generally faster and easier to use. See robots.thoughtbot.com/the-unix-shells-humble-if

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.