I did the following:
mapfile -t array < <(commands)
where commands will produce new-line separated output.
Then based on the result, if there's nothing in the array, exit, otherwise continue:
[[ "${#array[@]}" -eq 0 ]] && exit
...
The problem is that the condition is never met. Because when the said commands returns nothing, the ${#array[@]} returns 1 instead of 0.
But it counts correctly if I assign the array as follows:
array=($(commands))
My tests:
$ declare -a array
$ array=($(echo))
$ echo ${#array[@]}
0
$ unset array
$ mapfile -t array < <(echo)
$ echo ${#array[@]}
1
Why?
Initially I used the array=($(commands)) format but shellcheck suggested me to use mapfile instead.
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
mapfileinmapfile -t array < <(echo). It's not nothing - compare with the output of, say,trueor:.commandsdon't output newline-separated lines. So this comes down to those particular commands. Without them, we can't help you solve this issue.[[ "${array[0]}" == "" ]] && exit