我已经为这件事挣扎了一个多小时,我不知道怎么回事。使用Perl,我试图使用sed在/etc/nginx/nginx.conf中执行字符串的内联替换,如下所示:
my $replacement_string = getstringforreplace();
my $command = qq ( sudo sed -i "s~default_type application/octet-stream;~default_type application/octet-stream;$replacement_string~" /etc/nginx/nginx.conf );
system ( $command );
die ( $command ); # Using this for debugging purposes.我真的在尝试在nginx.conf中匹配“默认类型”行之后放置nginx.conf,但是我不知道除了sed还应该使用什么。
我已经(1)更改了分隔符以避免正斜杠出现任何问题,(2)双引号替换(我真的不知道为什么,我以前使用过单引号),(3)删除了$replacement_string之前的换行符,以及其他内容。
我先把模具( $command )放进去,就像在this答案中提到的那样,但我不知道出了什么问题。这就是我想要的结果
sudo sed -i "s~default_type application/octet-stream;~default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
tserver_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
~" /etc/nginx/nginx.conf$replacement_string是通过调用下面的子程序subroutine for替换()返回的:
sub getstringforreplace
{
my $message = qq (
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
tserver_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
);
return $message;
}任何指导都将是非常感谢的,因为我不知道如何消除这个未终止的命令问题。我在想,这跟我调用的子程序中的qq()有关。
发布于 2018-02-20 00:57:23
多亏了@Beta上面的评论,我才能得到我想要的结果。它涉及:
...which如下所示:
getstringforreplace(); # Prints $replacement_string to temp.txt.
my $command = qq ( sudo sed -i -e '/octet-stream;/r temp.txt' /etc/nginx/nginx.conf );
system ( $command );
system ( 'sudo rm temp.txt' );理想情况下,我希望不必打印到一个文件,等等,但目前这会产生预期的结果。
发布于 2018-02-19 06:21:22
sed不喜欢替换文字中的换行符。
$ sed 's~a~b~' /dev/null
$ sed 's~a~b
~' /dev/null
sed: -e expression #1, char 5: unterminated `s' command它确实接受\n,所以您可以用\n替换换行符。当然,您可以简单地使用Perl来完成这项工作。这将帮助您解决其他一些问题:
\。https://stackoverflow.com/questions/48860219
复制相似问题