I'm writing a script that sends the result of commands to an output array. It involves checking servers for logs and retrieving their sizes, however there are cases in which the server has a failover host. In these cases I need the script to check both hosts and return the values. The issue I'm running into is if any of the output array elements are empty (meaning there is no file existing in the server to check, returning just a space in its respective echo display), it causes a shift in the array indices. This means that what should be in the secondary host array set gets bumped into the primary one. I would instead like to have these empty indices be stored as 0 as a placeholder in order to prevent the shifting taking place. This is where the index shift is apparent:
echo The primary overall log value is ${output[0]}.
echo The primary obs log value is ${output[2]}.
echo The primary tracks log value is ${output[4]}.
echo
echo The secondary overall log value is ${output[6]}.
echo The secondary overall log value is ${output[8]}.
echo The secondary overall log value is ${output[10]}.
What happens for example is if output[2] and [4] are empty, output[6] will shift up to the line corresponding to output [2]. I've tried this solution but with no luck:
s=0
for x in "${output[@]}"
do(
x=
if [[ -z $x ]];
then(
x=0
echo $x)
else echo
fi
s=s+1)
done
All this does is spit out zeros before the echo of the outputs takes place, and does not adjust for any of the index shift.
Note: this is where the output array comes from, along with an attempt at a fix below it:
for h in "${host[@]}"
do
for path in "${paths[@]}"
do
output+=( $(ssh $h du -sh $path) )
for x in "${!output[@]}";
do(
if [[ -z "${output[$x]}" ]];
then output[$x]=0;
fi;
)
done
done
done
Where host and path are already defined arrays. I'd also note that the script functions fine for the cases in which there is only one host to access, and there is no issue with array indices there.