Possible Duplicate:
bash: double or single bracket, parentheses, curly braces
Looking at the rc.d
cron script in archlinux:
#!/bin/bash
. /etc/rc.conf
. /etc/rc.d/functions
name=crond
. /etc/conf.d/crond
PID=$(pidof -o %PPID /usr/sbin/crond)
case "$1" in
start)
stat_busy "Starting $name daemon"
[[ -z "$PID" ]] && /usr/sbin/crond $CRONDARGS &>/dev/null \
&& { add_daemon $name; stat_done; } \
|| { stat_fail; exit 1; }
;;
While I can figure out most of the syntax, what the heck does this do:
[[ -z "$PID" ]]
I saw that also written as:
[ -z "$PID" ]
In reference I found that []
is used in if-statements, but I see none here. Any help is much appreciated. Thanks!
The square brackets are a synonym for the test command. An if statement checks the exit status of a command in order to decide which branch to take. grep -q "$text" is a command, but "$name" = 'Bob' is not--it's just an expression.
$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.
Parentheses are often used for enclosing conditions and for function parameters. Braces are used for function bodies and objects. And brackets are used for lists or array notation. In Bash, that's often a little different and in many cases, parentheses, braces, and brackets act as commands themselves.
Example of command substitution using $() in Linux: Again, $() is a command substitution which means that it “reassigns the output of a command or even multiple commands; it literally plugs the command output into another context” (Source).
The opening bracket ([) is an alias for the test command which performs all the tests and returns 0 for true or something else for false. The "if" reacts only to the return value of the test command. The closing bracket tells test where the expression ends. The double brackets ([[) are a bash built in and can replace the external call to test.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With