Linux BASH - 如何获取 EOF 标签内的变量值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17983586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:25:32  来源:igfitidea点击:

BASH - how can i get the variable value inside the EOF tags?

linuxbashamazon-ec2vpnopenvpn

提问by

I have this following script but i need to get $i variable value working inside that each block starting with EOF and ending with EOF.

我有以下脚本,但我需要让 $i 变量值在每个块中工作,以 EOF 开头并以 EOF 结尾。

its not reading the variable value but putting $i

它不是读取变量值而是将 $i

/var/tmp/vpn.sh i have:

/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 &

采纳答案by Pikrass

Remove the backslash before EOF:

删除之前的反斜杠EOF

#!/bin/bash

i=ok

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

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


To get your last line display rightsubnet="10.109.0.20/32"(for i=1), you need something like this:

要显示最后一行rightsubnet="10.109.0.20/32"(对于 i=1),您需要这样的东西:

i=1
val1=beep
val2=bop

rightval="val$i"
cat <<EOF
This is a beep: ${!rightval}
EOF

That is, you compute the name of the variable you want, put that in another variable, and use the ${!var}syntax.

也就是说,您计算所需变量的名称,将其放入另一个变量中,然后使用${!var}语法。

But for that kind of thing you should rather use an array:

但是对于那种事情,您应该使用数组:

i=0
vals=(beep bop)

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

Note however that the indexes start at 0.

但请注意,索引从 0 开始。