Reputation: 74
I'm begginner on shell, currently i wrote a small script and i got a problem without any error :/
This code always exit my script and i dont understand why :
[[ -x $PATH ]] || log_failure_msg "Binary file not found or not executable!"; exit 0
When $PATH is valid i got nothing and if the path is wrong i got my failure message.
If i remove log_failure_msg "Binary file not found or not executable!";
the script work perfectly -_-
Ho i can solve this problem without if/fi conditions?
Thank you for your help!
Upvotes: 2
Views: 73
Reputation: 13924
[[ -x $PATH ]] || log_failure_msg "Binary file not found or not executable!"; exit 0
is equivalent to
{ [[ -x $PATH ]] || log_failure_msg "Binary file not found or not executable!" } ; exit 0
What you need is
[[ -x $PATH ]] || { log_failure_msg "Binary file not found or not executable!"; exit 0 }
I'm assuming you are using bash. The bash man page states:
..., && and || have equal precedence, followed by ; and &, which have equal precedence.
Upvotes: -1
Reputation: 2634
The issue is precedence, as explained by phlogratos. However, you can't use parenthesis as they spawn a sub-shell and you'll be exiting that shell. For this particular issue, curly braces exist. They have almost the same semantics but they spawn jobs in the current shell.
$ cat a.sh
[[ -f file ]] || { echo error; exit 0; }
echo "ok"
$ touch file
$ ./a.sh
ok
$ rm file
$ ./a.sh
error
$
Upvotes: 3