loops - Using Variables inside one another Bash -
i running loop calls in variable file:
hostnames.txt
the variables in file this:
export hostname_0=<hostname> export hostname_1=<hostname> export hostname_2=<hostname>
i trying use them inside loop this:
. hostnames.txt f in {0..10}; ssh $hostname_'{$f}' if [$? !=0]; echo "unable ssh server $hostname_'{$f}'" exit 1 fi
however not reading value correctly, , i'm not sure i'm missing. love help.
you need build variable name , access ${!var}
, e.g.:
. hostnames.txt f in {0..10}; hostnamevar=hostname_${f} ssh ${!hostnamevar} if [ $? -ne 0 ]; echo "unable ssh server ${!hostnamevar}" exit 1 fi done
Comments
Post a Comment