I am having trouble understanding the efficient use of global variables. From my understanding of bash, each variable is global unless explicitly declared locally: http://tldp.org/LDP/abs/html/localvar.html . Thus, I realized that if I create such a function:
is_alive_ping()
{
declare -a ip_range=("${!1}")
declare -a active_ips
for i in "${ip_range[@]}"
do
echo "Pinging host: " $i
if ping -b -c 1 $i > /dev/null; then
active_ips=("${active_ips[@]}" "$i")
echo "Host ${bold}$i${normal} is up!"
fi
done
}
I should be able to access the variable active_ipsafter calling the is_alive_ping function. For example:
is_alive_ping ip_addr_range[@]
echo ${active_ips[*]}
stackoverflow: Bash Script - . active_ips . , , IP-. , ?