Reputation: 695
Looking at correcting an issue in /etc/init.d/hostapd on Debian. However, I have no clue what this line of code does nor how it works
[ -n "$DAEMON_CONF" ] || exit 0
In searching online for bash tutorials, I've never seen anyone do this
When I run the code, my shell window closes (because $DAEMON_CONF is not set to anything). If I change the code to
[ -n "not empty" ] || exit 0
my console window does not close.
so, -n evaluates to true, and or'ed with exit 0, is what?
Upvotes: 6
Views: 2362
Reputation: 1683
It checks if the environment variable is defined, if $DAEMON_CONF is not present the it will exit with 0 code, a better code would be.
[ -n "$DAEMON_CONF" ] || echo "exiting as DAEMON_CONF is not set" && exit 1
Upvotes: 0
Reputation: 8406
[ -n "$DAEMON_CONF" ] || exit 0
It's an unnecessary double negative. This would do the same thing:
[ -z "$DAEMON_CONF" ] && exit 0
Or it could be done without any flag:
[ "$DAEMON_CONF" ] || exit 0
Upvotes: 3
Reputation: 50200
[
is and alternate name for the command test
. You can learn about the parameters/flags whatnot by looking at test's manpage:
man test
You'll see for -n
:
-n STRING
the length of STRING is nonzero
Furthemore ||
means OR
. So if the test
command returns False
then the stuff after the ||
will be executed. If test
returns true, then it won't be executed.
Written out your command says: "If the variable $DAEMON_CONF lacks a value, then exit with return code 0"
The longhand version would be something like:
if test ! -n "$DAEMON_CONF"; then
exit 0
fi
Upvotes: 4
Reputation: 201467
If the expression in []
returns false, do the thing after the or ||
(and exit 0
). Otherwise, it will short circuit and the next statement will be evaluated.
Upvotes: 7