0

场景:尝试使用 sed 修改大部分 httpd 配置文件。

我试过的:

read -d '' _OLD <<"EOF"
<Directory "/var/www/html">

#
# Possible values for the Options directive are "None", "All",
# or any combination of:
#   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important.  Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information.
#
    Options Indexes FollowSymLinks

#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
#   Options FileInfo AuthConfig Limit
#
    AllowOverride None
EOF

read -d '' _NEW <<"EOF"
<Directory "/var/www/html">

#
# Possible values for the Options directive are "None", "All",
# or any combination of:
#   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important.  Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information.
#
    Options Indexes FollowSymLinks

#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
#   Options FileInfo AuthConfig Limit
#
    AllowOverride All
EOF

echo -n ${_OLD}
echo -n ${_NEW}
sed -i "s/$_OLD/$_NEW/g" /etc/httpd/conf/httpd.conf

如果您注意到唯一改变的是AllowOverride All我只想为/var/www/html其他实例而不是为其他实例修改规则,因此我为什么要查看整个块。

我知道可能有更好的方法来做我想要完成的事情。方向将不胜感激。

4

3 回答 3

2
sed -r '\%<Directory "/var/www/html">%,\%</Directory>% s%(AllowOverride)\s+None%\1 All%i'
于 2013-02-08T02:33:08.287 回答
1

好吧,我的(过期)版本sed不允许多行“旧”模式(正则表达式),并且需要多行替换在换行符之前有反斜杠,如

sed "s/lifetime/birth\
…\
death/"

……所以这是一两个问题。另一个是您将/其用作分隔符,即使您的模式和替换中有斜杠。

我建议你使用正则表达式地址:

sed '/<Directory "\/var\/www\/html">/,/AllowOverride/s/AllowOverride None/AllowOverride All/'

换句话说,在一行和下一之间s/AllowOverride None/AllowOverride All/任何适用行上进行替换。(注意第一个正则表达式中的引号。) <Directory "/var/www/html"> AllowOverride

于 2013-02-08T02:24:44.057 回答
0

为什么不使用正则表达式?Sed 本身就支持它们。 更多细节在这里

于 2013-02-08T02:20:26.350 回答