Test if two variable are empty at the same time

你离开我真会死。 提交于 2019-12-13 14:25:56

问题


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 ?


回答1:


You can use this combined test:

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

OR separate tests in same if condition:

[[ -z "$var1" && -z "$var2" ]]



回答2:


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


来源:https://stackoverflow.com/questions/27291287/test-if-two-variable-are-empty-at-the-same-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!