Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在bash中的herdoc中,my$PORT变量不起作用_Bash_Shell_Heredoc - Fatal编程技术网

在bash中的herdoc中,my$PORT变量不起作用

在bash中的herdoc中,my$PORT变量不起作用,bash,shell,heredoc,Bash,Shell,Heredoc,我在网上找不到关于这方面的任何信息,但我可以发现我不应该在EOT上使用引号,但在我的情况下,我不这样做,所以如果有人能在这里帮助我,那将是非常棒的 这是脚本的一部分,用于安装新的Debian安装 问题: 运行时,我无法访问cat/EOThere文档中的$PORT。 setUPiptables() { if ! grep -e '-A INPUT -p tcp --dport 80 -j ACCEPT' /etc/iptables.up.rules then ca

我在网上找不到关于这方面的任何信息,但我可以发现我不应该在
EOT
上使用引号,但在我的情况下,我不这样做,所以如果有人能在这里帮助我,那将是非常棒的

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

问题: 运行时,我无法访问
cat/EOT
here文档中的$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
}
setUPiptables()
{
if!grep-e'-A INPUT-ptcp--dport 80-j ACCEPT'/etc/iptables.up.rules
然后
cat>/etc/iptables.test.rules
*滤器
IPTABLES-CODE-HERE
#允许SSH连接
#--dport编号与/etc/ssh/sshd\u config中的相同
-输入-p tcp-m状态--state NEW--dport$PORT-j ACCEPT
IPTABLES-CODE-HERE
犯罪
EOT
睡眠5
/sbin/iptables还原/etc/iptables.up.rules | |退出127
睡眠3
printf“#!/bin/bash\n/sbin/iptables restore/etc/network/if-pre-up.d/iptables
chmod+x/etc/network/if-pre-up.d/iptables
睡眠6
fi
}
问题:


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

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


cat/etc/iptables.test.rules

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


cat/etc/iptables.test.rules
我很抱歉在这个问题上花了很多时间。我犯了一个初学者的错误,我读的是
grep
中的文件名,而不是实际的
文件(
/etc/iptables.test.rules
),因此,我在
iptables save
试图与$PORT
duplicates
一起使用的实际文件中多次连接
HERE-DOC
,但由于所有额外的代码(胡言乱语),它失败了

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

因此,我没有创建/code检查iptables是否已设置,文件
/etc/iptables.test.rules
是否存在,因此我将double
iptables
代码添加到一个已经包含我正在编写的代码的文件中


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

我很抱歉在这个问题上花了很多时间。我在阅读
grep
中的文件名,而不是实际的
文件(
/etc/iptables.test.rules
),这是一个初学者的错误,因此,我在
iptables save
试图与$PORT
duplicates
一起使用的实际文件中多次连接
HERE-DOC
,但由于所有额外的代码(胡言乱语),它失败了

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

因此,我没有创建/code检查iptables是否已设置,文件
/etc/iptables.test.rules
是否存在,因此我将double
iptables
代码添加到一个已经包含我正在编写的代码的文件中


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

?这根本不是取消对变量的引用,而是将文本
$PORT
放入herdoc。如果OP希望他们的heredoc只是文字,更好的方法是改变
@CharlesDuffy我也有同样的问题,对我来说,这起作用了。我不相信你的问题是真的一样。OP正在运行
read-r端口;PORT=$PORT
在生成heredoc之前,他们希望从用户读取的值(而不是变量引用)进入heredoc。相比之下,如果OP想要的是不取消引用它,他们的问题是重复的(我们在知识库中已经有很多关于这个的条目)。例如,参见……并注意到……的“回答好问题”部分中关于“以前已经被问过并回答过多次”的问题的要点。嗯?这根本不是取消对变量的引用,而是将文本
$PORT
放入herdoc。如果OP希望他们的heredoc只是文字,更好的方法是改变
@CharlesDuffy我也有同样的问题,对我来说,这起作用了。我不相信你的问题是真的一样。OP正在运行
read-r端口;PORT=$PORT
在生成heredoc之前,他们希望从用户读取的值(而不是变量引用)进入heredoc。相比之下,如果OP想要的是不取消引用它,他们的问题是重复的(我们在知识库中已经有很多关于这个的条目)。例如,请参见……并注意本节“回答好问题”一节中关于“以前已经被问过并回答过多次”的问题的要点。