Mailinglist Archive: opensuse (4020 mails)
| < Previous | Next > |
Re: [SLE] Bash Q: how to keep a script from failing when a var is undef or empty
- From: ti@xxxxxxx (Ti Kan)
- Date: Fri, 1 Oct 2004 00:49:33 -0700 (PDT)
- Message-id: <20041001074933.DDC30169A1@xxxxxxxxxxxx>
JW writes:
> TAPESTATE=`mt -f /dev/st0 status | head -2 |tail -1 | cut -d " " -f4`
>
> if [ ${TAPESTATE} == "512" ]; then
> READY="yes"
> else
> READY="no" && echo "No tape in oe1 tape drive" | mail -s "Oe1 tape error"
> jw@xxxxxxxxxx
> fi
A general way to test for a non-empty string is:
if [ -n "${TAPESTATE}" ]; then
...
fi
Or its converse, to test for an empty string:
if [ -z "${TAPESTATE}" ]; then
...
fi
See "man test". The left bracket [ is the same thing as the "test" command.
-Ti
> TAPESTATE=`mt -f /dev/st0 status | head -2 |tail -1 | cut -d " " -f4`
>
> if [ ${TAPESTATE} == "512" ]; then
> READY="yes"
> else
> READY="no" && echo "No tape in oe1 tape drive" | mail -s "Oe1 tape error"
> jw@xxxxxxxxxx
> fi
A general way to test for a non-empty string is:
if [ -n "${TAPESTATE}" ]; then
...
fi
Or its converse, to test for an empty string:
if [ -z "${TAPESTATE}" ]; then
...
fi
See "man test". The left bracket [ is the same thing as the "test" command.
-Ti
| < Previous | Next > |