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

问题描述 投票:0回答:2

我在网上找不到任何关于此的内容,但我发现我不应该在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
}

题:

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

bash shell heredoc
2个回答
1
投票

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

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


1
投票

我为在这个问题上吸引人们的时间而道歉这是一个初学者的错误,我正在读grep中的文件名而不是实际的file/etc/iptables.test.rules),所以我在HERE-DOC尝试使用的实际文件中多次连接iptables-save使用$ PORT duplicates,当然它失败了所有额外的代码(乱码)。

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.