Test if $1 OR $2 are null with “-z” – BASH

I’m doing multiple raid monitoring in the same script and I want to have the script send alert/go red if EITHER variable comes back null.

I tried reading up and thought I had it, but what I tried ended up just never failing.

For testing, I have it grep fail and this SHOULD cause it to fail, but so far, I can’t get it to fail, it actually just always passes.

The Test Environment looks like this :

var="$(sudo /usr/StorMan/arcconf GETCONFIG 1 LD 0 | grep Optimal)"
var1="$(sudo /usr/StorMan/arcconf GETCONFIG 1 LD 1 | grep fail)"

This works for 1 Variable

if [ -z "$var" ]

I have tried

if [ -z "$var" ] && [ -z "$var1" ]
if [ -z "$var" && -z "$var1" ]
if [[ -z "$var" && -z "$var1" ]]

But to no avail, I’m sure somebody would know what I’m doing wrong in a heartbeat, I appreciate the time taken to read this!

Answer

Use || rather than &&, e.g.,

if [ -z "$var" ] || [ -z "$var1" ]

The bash manual explains it:

AND and OR lists are sequences of one or more pipelines separated by the control operators && and ||, respectively. AND and OR lists are executed with left associativity.

Leave a Reply

Your email address will not be published. Required fields are marked *