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!

4 Answers 4


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.


What you are asking for has already been mentioned:

if [ -z "$var" ] || [ -z "$var1" ]; then
  echo "one or both are empty."

However, it's worth mentioning that if this is the only use you are making of these variables, you don't need to set them as variables.

You can check the exit status of a command directly in if constructs—and in fact [ is a command, not a special shell token. (It's exactly equivalent to the command test; see man [)

As I understand it, the output of your first sudo command MUST contain "Optimal" and the output of the second MUST contain "fail", otherwise you want to trigger an alert.

You could go about this much more clearly in my opinion like so:

if sudo /usr/StorMan/arcconf GETCONFIG 1 LD 0 | grep -q Optimal &&
   sudo /usr/StorMan/arcconf GETCONFIG 1 LD 1 | grep -q fail
  echo "All is well."
  echo "Error condition!"
  • The reason I use variables is the script is for xymon and I change the command that the script executes by changing var but leave the rest of the script alone. This allows me to modify the script easily to Monitor other tasks like my UPS status etc etc. But it is always good to learn different approach to scripting. Aug 6, 2016 at 5:33
  • @FreeSoftwareServers, okay, I just hope you clean it up and get it reviewed on CodeReview before you release it to Production. As a sysadmin I've seen too many times where application developers considered such things as startup scripts as an unimportant afterthought...so their applications can't be easily integrated with init to start on boot. Of course, you're a step ahead of them already, because you're asking a question on U&L Stack Exchange. ;)
    – Wildcard
    Aug 6, 2016 at 5:37
  • Its a fairly simple script, I think you'll understand why I leave it with var, I have quite a few scripts I implement and I love that all I change is var=command | grep status. freesoftwareservers.com/index.php/2016/08/06/… Aug 6, 2016 at 5:48
  • @FreeSoftwareServers, okay, but there's no such service as sudo. (I see you have a line to run sudo service sudo restart, which is nonsense.) And your status thing could be more easily handled and modified with a shell function. If you post something on code review and link to it here, I'll post a review there.
    – Wildcard
    Aug 6, 2016 at 5:53
  • sudo service sudo restart allows me to immediatley grant xymon passwordless access to the arcconf utility (after editing sudoers), otherwise, I believe it requires a reboot, but I have to test. There IS an init.d script for sudo. The reason I don't want to change the script as its from XYMon.com/custom_scripts and its the same across our company, we use the same script, but only edit var=$cmd. Aug 6, 2016 at 5:57

If you're after "either" becoming empty, perhaps try OR ||?

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

... Can it be that easy?

  • This is likely what I will use. It is easy to just add another variable without changing the existing line of code. I like that if I say need to Monitor a 3rd raid one day. Aug 6, 2016 at 5:36

Two solutions are:

if [[ -z $a || -z $b ]];      ### (not A) OR (not B)
if ! [[ $a && $b ]];          ### not (A and B)

more portable:

if [ -z "$a" ] || [ -z "$b" ];
if ! ( [ "$a" ] && [ "$b" ] );

This options may work but are rejected by POSIX (>4 arguments: The results are unspecified.) :

if [ -z "$a" -o -z "$b" ];
if [ ! \( "$a" -a "$b" \) ];

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .