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

Scope inside a heredoc in bash, my $PORT variable is not working

我在网上找不到任何关于此的内容,但我发现我不应该在 EOT 上使用引号,但在我的情况下,如果有人可以帮助我的话,我不会会很棒.........

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

问题: cat/EOT here-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/bash\n/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
}

问题:

你能find/see cat iptables 代码中 $PORT 的问题吗?

尝试使用,因为这是 question:

的重复答案

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

我很抱歉在这个问题上占用了大家的时间,这是一个初学者的错误,我读的是 grep 中的文件名,而不是实际的 file(/etc/iptables.test.rules),所以我在 iptables-save 试图与 $PORT duplicates 一起使用的实际文件中多次连接 HERE-DOC 并且当然它因所有额外代码(乱码)而失败。

问题已解决......来自冰岛的抱歉。

所以我没有 create/code 检查 iptables 是否已设置以及文件 /etc/iptables.test.rules 是否存在,所以我将双 iptables 代码附加到一个已经包含代码 I 的文件中正在写作。

感谢@CharlesDuffy 抽出宝贵时间 advice/guidance