1
\$\begingroup\$

I'd like to see if there are any suggestions on how to improve a simple conditional test at the beginning of a backup script that verifies a ZFS volume is "online" and has the name "tank".

For example:

% zpool list
NAME      SIZE  ALLOC   FREE  EXPANDSZ   FRAG    CAP  DEDUP  HEALTH  ALTROOT
tank  54.5T  36.0T  18.5T         -    35%    66%  1.00x  ONLINE  -

The following simple script works, but I suspect there may be a more elegant way to do this. ShellCheck notes $STATUS is unused...

#!/bin/sh
#
STATUS=$(/sbin/zpool list | grep tank | grep ONLINE)
if [ $? -gt 0 ]; then
        echo "Problems"
        exit 1
else

echo all system go!
exit 0

fi
\$\endgroup\$
2
\$\begingroup\$

You can simplify the zpool command, by specifying the volume name as an argument:

/sbin/zpool list tank | grep ONLINE

This will avoid cases where tank might be part of another volume's name. Better yet, use zpool get:

/sbin/zpool get -H health tank

This returns output which is more specific and easier to parse, since the status is the third, tab-separated field:

tank     health    ONLINE   -

So:

STATUS=$(/sbin/zpool get -H health tank | awk '{print $3}')

Then, you probably should test the value of STATUS, instead of the exit code:

if [ "$STATUS" != ONLINE ]; then
        echo "Problems"
        exit 1
...

Next, be consistent in indentation:

if [ "$STATUS" != ONLINE ]; then
        echo "Problems"
        exit 1
else
        echo "All systems go!"
        exit 0
fi

Also: all system go! should be All systems go!.

exit 0 is unnecessary in this case, without it, the shell will exit the else block and then the script itself, and return the exit status of the last command executed (echo, which would return 0 unless it couldn't write output, in which case you probably shouldn't have the echo there anyway).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.