linux – 如何在shell脚本中包含目录中的所有文件(在本例中为/etc/init.d/iptables)

我在不同的ubuntu服务器上有一个/etc/init.d/iptables start | stop | restart脚本(这是一个普通的 shell脚本)

对于每个新服务,我必须编辑并插入一行来打开一个端口.这导致在不同的机器上有许多不同版本的init.d脚本.

是否可以自动包含让我们说/etc/iptables/include.d/中的所有文件?

目标是/etc/init.d/iptables的启动函数中只应该有一行代码

include /etc/iptables/include.d/*

在/etc/iptables/include.d/中添加了一个额外的文件后,我只想说

/etc/init.d/iptables restart

编辑:正如Saurabh所指出的那样,当命令需要某个命令时,这会导致问题.高级设置可能有不同的目录,如:

/etc/iptables/include01.d/
/etc/iptables/include02.d/
/etc/iptables/include03.d/

包括他们像这样:

    include /etc/iptables/include01.d/*
    ... maybe some code goes here in the main file...
    include /etc/iptables/include02.d/*
    include /etc/iptables/include03.d/*

解决方法

将以下行添加到init.d脚本中.
run-parts --report /etc/iptables/include.d

它将作为shell脚本运行目录中的所有内容(需要可执行).

如果您只想执行以.port结尾的文件,您可以使用某些东西
喜欢:

run-parts --regex '\.port$' /etc/iptables/include.d/

如果您想确保订单正确,可以为文件命名:

10_web.port
20_ssh.port
etc..

相关文章

文章浏览阅读1.8k次,点赞63次,收藏54次。Linux下的目录权限...
文章浏览阅读1.6k次,点赞44次,收藏38次。关于Qt的安装、Wi...
本文介绍了使用shell脚本编写一个 Hello
文章浏览阅读1.5k次,点赞37次,收藏43次。【Linux】初识Lin...
文章浏览阅读3k次,点赞34次,收藏156次。Linux超详细笔记,...
文章浏览阅读6.8k次,点赞109次,收藏114次。【Linux】 Open...