【发布时间】:2019-09-05 02:52:45
【问题描述】:
我正在创建一个脚本来使用黑名单填充 htaccess 文件。为此,我使用 curl 下载了一个黑名单,我使用 while 语句将其读入数组,这样我就可以忽略不以“拒绝”开头的行。现在我想将数组中的元素插入到.htaccess 文件中。
该数组名为DENY_DATA,包含如下所示的条目:
deny from 64.62.252.163
deny from 167.99.255.132
deny from 27.209.94.238
deny from 64.188.8.229
...
我已经在.htaccess 文件中插入了两行,我希望拒绝语句在它们之间。它们看起来像这样:
#~~~~~~~~~ BLACKLIST START ~~~~~~~~~~
#********* BLACKLIST END **********
我使用 awk 尝试过以下操作。但是,这会导致 tmp 文件仅包含 ${DENY_DATA[@]} 作为字符串。
awk '/^#~~~~~~~~~/ { printf "%s\n", "${DENY_DATA[@]}" }' .htaccess > tmp.file
我也按照Inserting multiline text from file to another file after pattern 和Bash script to insert code from one file at a specific location in another file? 尝试过使用sed。有了这个,我留下了原始的 htaccess 文件,但没有拒绝声明。
sed -e '/^#~~~~~~~~~/r printf "%s\n" "${DENY_DATA[@]}"' .htaccess > tmp.file
有没有简单的解决方法,或者有更好的方法来解决这个问题?
【问题讨论】: