在 bash 中的 heredoc 内范围,我的 $PORT 变量不起作用



我在网上找不到任何关于这个的东西,但我可以找到我不应该在EOT上使用引号,但就我而言,我没有,所以如果有人能在这里帮助我,那就太棒了......

这是用于设置新Debian安装的脚本的一部分

问题: 当cat/EOThere-document运行时,我无法访问$PORT。

setUPiptables()
{
if ! grep -e '-A INPUT -p tcp --dport 80 -j ACCEPT' /etc/iptables.up.rules
then
cat << EOT >> /etc/iptables.test.rules
*filter

IPTABLES-CODE-HERE
# Allows SSH connections
# The --dport number is the same as in /etc/ssh/sshd_config
-A INPUT -p tcp -m state --state NEW --dport $PORT -j ACCEPT

IPTABLES-CODE-HERE
COMMIT
EOT
sleep 5
/sbin/iptables-restore < /etc/iptables.test.rules || exit 127
sleep 5
/sbin/iptables-save > /etc/iptables.up.rules || exit 127
sleep 3
printf "#!/bin/bashn/sbin/iptables-restore < /etc/iptables.up.rules" > /etc/network/if-pre-up.d/iptables
chmod +x /etc/network/if-pre-up.d/iptables
sleep 6
fi
}

问题:

你能在catiptables代码中找到/看到$PORT的问题吗?

尝试使用,因为这是这个问题的重复答案:

cat <<'EOT' >> /etc/iptables.test.rules

我很抱歉在这个问题上花时间,这是一个初学者的错误,我正在阅读grep中的文件名而不是实际的file(/etc/iptables.test.rules(,所以我在iptables-save尝试使用的实际文件中多次连接HERE-DOC$PORTduplicates当然它失败了所有额外的代码(胡言乱语(。

问题解决了...对不起,来自冰岛。

所以我没有创建/编码检查是否设置了iptables并且文件/etc/iptables.test.rules存在,所以我将双iptables代码附加到已经包含我正在编写的代码的文件中。

感谢您@CharlesDuffy的时间和建议/指导

相关内容

最新更新