简体   繁体   English

nodejs的“ exec”命令未正确执行“ sed”命令

[英]nodejs 'exec' command doesn't execute 'sed' command properly

I have a file where I want to scan it, find the character '{' and create a new line, then add an IP on the new line and add a semi-cologne to the end of the line, then write to a config file. 我有一个要扫描的文件,找到字符“ {”并创建新行,然后在新行上添加IP,并在行末添加半科隆,然后写入配置文件。

I can accomplish this with the following sed command when running from shell: 从shell运行时,可以使用以下sed命令完成此操作:

sed -i 's/{/&\n1.1.1.1;/g' /tmp/test.conf

inside test.conf: 在test.conf内部:

acl testACL{
};

the output from the command in shell shows: Shell中命令的输出显示:

acl testACL{
1.1.1.1;
};

works perfect! 完美的作品! Now the problem is when I get nodejs to execute it: 现在的问题是当我让nodejs执行它时:

        var sys = require('sys');
        var exec = require('child_process').exec;
        function puts(error, stdout, stderr) { sys.puts(stdout) };
        exec("sed -i 's/{/&\n1.1.1.1;/g' /tmp/test.conf",puts);
        return 0;

when I run the command in console: 'nodejs test.js' 当我在控制台中运行命令时:“ nodejs test.js”

I get blank output and when I check the file, the file 'test.conf' has not been altered! 我得到空白输出,当我检查文件时,文件“ test.conf”未被更改! why?! 为什么?!

Also if you're thinking 'its a permissions issue!' 另外,如果您正在考虑“这是权限问题!” I've had nodejs exec command write basic echos to the test config file and it worked fine. 我已经用nodejs exec命令将基本回显写入测试配置文件,并且工作正常。

I have also tried the 'shelljs' module with no luck there. 我也尝试过'shelljs'模块,但没有运气。 I have tried countless combinations for hours now with no prevail! 我已经尝试了数小时的无数组合,但没有成功! I'm puzzled. 我很困惑

I think you need to escape your \\n as \\\\n . 我认为您需要将\\n\\\\n Right now, it's getting parsed as a literal newline in the command, which is messing it up. 现在,它被解析为命令中的文字换行符,这很混乱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM