Reputation: 1001
Complete noob in bash. Trying to edit existing script. I've Googled things like "Bash operators" and I can't find this one.
Here's the snippet:
if [ "${activ_con}" -a ! "${activ_vpn}" ];
then
nmcli con up id "${VPN_CONNECTION_NAME}"
fi
I just want to know what the "-a !" bit is doing. Thanks. (like always, I'll probably find it in the next few minutes... back to GOogle!)
Upvotes: 1
Views: 138
Reputation: 647
-a and -o are so called compound statement. -a is same as logical "&&" operator and -o is the same as logical "||" operator. here is the difference:
if [conditionA -o conditionB]
compared to:
if [conditionA] || [conditionB]
Same case for -a and &&.
Upvotes: 0
Reputation: 754190
It (-a
) is the 'and' operator, and -o
is the 'or' operator.
An alternative is to use:
if [ -n "${activ_con}" ] && [ -z "${activ_vpn}" ]
then
nmcli con up if "${VPN_CONNECTION_NAME}"
fi
This explicitly tests for a non-zero length of "${activ_con}"
and for the zero length of "${active_vpn}"
. This is more nearly the style recommended by the Autoconf shell style rules (but they don't use [
for test at all because the square brackets are a metacharacter. Some old implementations (in the 1980s and early 1990s, perhaps) had issues misinterpreting the operands to the test
command, so the advice given avoids using the -a
and -o
because these were the options that lead to confusion.
The POSIX specification for test
(and [
is a synonym for test
; on many systems, there is also a command /bin/[
that is a link to /bin/test
, but the shell normally uses a built-in implementation of test
) explicitly specifies that -a
is used as a connective meaning 'and'. (Note the rationale section of the test
page on the POSIX site.)
The GNU bash
specification for Conditional Expressions does not list -a
as a connective, nor does it list -o
at all. That's interesting.
Upvotes: 1
Reputation: 784
It means "and".
From the manual page for test:
expression1 -a expression2
True if both expression1 and expression2 are true.
The bracket is a synonym for test
.
Upvotes: 4