linux - BASH - how can i get the variable value inside the EOF tags? -
i have following script need $i variable value working inside each block starting eof , ending eof.
its not reading variable value putting $i
/var/tmp/vpn.sh have:
#!/bin/bash amazoneth0="10.0.0.18" amazonwan0="4.9.2.9" vpnserver="4.8.8.6" hosttohost1="10.109.0.20/32" hosttohost2="10.109.0.21/32" hosttohost3="10.109.58.6/32" hosttohost4="10.109.59.3/32" in 1 2 3 4 cat > /tmp/test$i.conf << \eof #step 3 conn test"$i" #auto=start type=tunnel authby=secret pfs=no aggrmode=no ikelifetime=28800s lifetime=3600s ike=aes128-md5;modp1024! phase2alg=aes128-md5;modp1024 forceencaps=yes left=$amazonlan0 leftid=$amazonwan0 leftsourceip=$amazonwan0 right=$vpnserver rightsubnet=$hosttohost$i eof done ### run me cat > /var/tmp/vpn.sh << \eof service ipsec restart ######## apply loop here, instead of many many lines ########### # in 1 2 3 4 # # ipsec auto --add test$i # done ipsec auto --add test1 ipsec auto --add test2 ipsec auto --add test3 ipsec auto --add test4 ######## apply loop here, instead of many many lines ########### # in 1 2 3 4 # # ipsec auto --up test$i # done ipsec auto --up test1 ipsec auto --up test2 ipsec auto --up test3 ipsec auto --up test4 ipsec auto --status ip xfrm policy ip route show ######## apply loop here, instead of many many lines ########### # in 1 2 3 4 # # ping -c 1 $hosttohost$i # done ping -c 1 10.109.0.20; ping -c 1 10.109.0.21; ping -c 1 10.109.58.6; ping -c 1 10.109.59.3; eof chmod +x /var/tmp/vpn.sh # cake - eat - optional /var/tmp/vpn.sh > save output | mail -s ipsec date time &
remove backslash before eof:
#!/bin/bash i=ok # prints "bwah ok" cat <<eof bwah $i eof # prints "bwah $i" cat <<\eof bwah $i eof to last line display rightsubnet="10.109.0.20/32" (for i=1), need this:
i=1 val1=beep val2=bop rightval="val$i" cat <<eof beep: ${!rightval} eof that is, compute name of variable want, put in variable, , use ${!var} syntax.
but kind of thing should rather use array:
i=0 vals=(beep bop) cat <<eof beep: ${vals[$i]} eof note indexes start @ 0.
Comments
Post a Comment