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

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Test if $1 OR $2 are null with “-z” – BASH without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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!


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.

We are here to answer your question about Test if $1 OR $2 are null with “-z” – BASH - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji