首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在sed命令中传递变量时未终止的干涉仪

在sed命令中传递变量时未终止的干涉仪
EN

Stack Overflow用户
提问于 2018-02-19 05:52:58
回答 2查看 54关注 0票数 0

我已经为这件事挣扎了一个多小时,我不知道怎么回事。使用Perl,我试图使用sed在/etc/nginx/nginx.conf中执行字符串的内联替换,如下所示:

代码语言:javascript
复制
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答案中提到的那样,但我不知道出了什么问题。这就是我想要的结果

代码语言:javascript
复制
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替换()返回的:

代码语言:javascript
复制
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()有关。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-02-20 00:57:23

多亏了@Beta上面的评论,我才能得到我想要的结果。它涉及:

  1. 将子程序temporary for重置()更改为将qq()的内容打印为临时文件,
  2. 使用sed进行就地替换,将该文件的内容读入/etc/nginx/nginx.conf,
  3. 然后删除临时文件。

...which如下所示:

代码语言:javascript
复制
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' );

理想情况下,我希望不必打印到一个文件,等等,但目前这会产生预期的结果。

票数 0
EN

Stack Overflow用户

发布于 2018-02-19 06:21:22

sed不喜欢替换文字中的换行符。

代码语言:javascript
复制
$ sed 's~a~b~' /dev/null

$ sed 's~a~b
~' /dev/null
sed: -e expression #1, char 5: unterminated `s' command

它确实接受\n,所以您可以用\n替换换行符。当然,您可以简单地使用Perl来完成这项工作。这将帮助您解决其他一些问题:

  • Shell命令注入错误。
  • 替换文本中缺少转义\
  • 缺乏错误检测和处理。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48860219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档