I like to make global variables for all my host names. The must get an variable name ip_ = ipadress I got it working partially...
The variable is lost out of the while done loop
#!/bin/bash
#file x.x
like to make global variables for all hostnames with ip_ in front of it...
#192.168.20.48 dockerhub
#192.168.20.48 mysqlserver
#192.168.20.48 mqttserver
#192.168.20.48 proxyserver
#192.168.20.48 domserver
#192.168.20.48 dockerserver
tr -d '\r' < /home/pi/iotmenu/testscripts/x.x |
while read kola kolb; do
if [[ $kola =~ ^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$ ]]; then
echo "1 $kolb $kola"
eval ip_$kolb=$kola
echo "2 ip_$kolb=$kola"
declare "ip_$kolb=$kola"
export "ip_${kolb}=${kola}"
echo "example_in ip_mysqlserver=$ip_mysqlserver"
fi
done
echo "example_out ip_mysqlserver=$ip_mysqlserver"
the variable is lost after the loop
declare -Ax ip
(-A for associative array, -x to export the array - seehelp declare
in bash). Later, in the loop:ip[$kolb]="$kola"
. – cas Mar 19 '23 at 01:38