问题
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