Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if two variable are empty at the same time

I would like to test both variable in the same if condition.

Currently I am using [[ $var ]] to test one but the same for two variable do not work.

In order to do that I tried :

if [[ &var1 && &var2 ]]; then
or 
if [[ &ipAddress ]] && [[ &bcastAddress ]]; then

Is there a limitation ?

like image 912
ogs Avatar asked Sep 03 '26 02:09

ogs


2 Answers

You can use this combined test:

[[ -z "${var1}${var2}" ]]

OR separate tests in same if condition:

[[ -z "$var1" && -z "$var2" ]]
like image 87
anubhava Avatar answered Sep 04 '26 22:09

anubhava


I often use arithmetic for these sorts of things. For example:

case "$(((!${#var1}&&!${#var2})*${#ip}))" in 
(0) ! echo ERROR;; 
(${#bcast}) echo '$ips len is equal to $bcasts and neither are zero';;
esac

Of course, you don't need the case there:

[ "${#var1}${#var2}" -eq "$(((${#ip}&&${#bcast})?0:-1))" ] || handle_it
like image 31
mikeserv Avatar answered Sep 04 '26 23:09

mikeserv