Linux 如何获取EOF标记中的变量值?

Linux 如何获取EOF标记中的变量值?,linux,bash,amazon-ec2,vpn,openvpn,Linux,Bash,Amazon Ec2,Vpn,Openvpn,我有下面的脚本,但我需要在每个块中使用$I变量值,从EOF开始,到EOF结束 它不是读取变量值,而是放入$i /var/tmp/vpn.sh我有: #!/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/3

我有下面的脚本,但我需要在每个块中使用$I变量值,从EOF开始,到EOF结束

它不是读取变量值,而是放入$i

/var/tmp/vpn.sh我有:

#!/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"

for i in 1 2 3 4
do
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 for loop here, instead of many many lines ###########
# for i in 1 2 3 4
# do
#   ipsec auto --add test$i
# done
ipsec auto --add test1
ipsec auto --add test2
ipsec auto --add test3
ipsec auto --add test4

######## Apply for loop here, instead of many many lines ###########
# for i in 1 2 3 4
# do
#   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 for loop here, instead of many many lines ###########
# for i in 1 2 3 4
# do
#   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 now - optional 
/var/tmp/vpn.sh > save output | mail -s ipsec date time &
#/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”
我在1234
做
cat>/tmp/test$i.conf/var/tmp/vpn.sh保存输出|邮件-s ipsec日期时间&

EOF
之前删除反斜杠:

#!/bin/bash

i=ok

# This prints "Bwah ok"
cat <<EOF
Bwah $i
EOF

# This prints "Bwah $i"
cat <<\EOF
Bwah $i
EOF
也就是说,计算所需变量的名称,将其放入另一个变量中,然后使用
${!var}
语法

但对于这种情况,您应该使用数组:

i=0
vals=(beep bop)

cat <<EOF
This is a beep: ${vals[$i]}
EOF
i=0
VAL=(嘟嘟声防喷器)

cat删除conn测试“$i”上的引号?好-但是$hosttoHost$i变为1而不是$hosttoHost1$hosttoHost2…$hosttoHost$i变为1而不是$hosttoHost1谢谢。我能不能在cat编辑中再做一遍:您可以使用数组,它更可读,并且您不必计算其他变量的名称。
i=0
vals=(beep bop)

cat <<EOF
This is a beep: ${vals[$i]}
EOF