Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

I can accomplish this with the following sed command when running from shell:

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

inside test.conf:

acl testACL{
};

the output from the command in shell shows:

acl testACL{
1.1.1.1;
};

works perfect! Now the problem is when I get nodejs to execute it:

        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'

I get blank output and when I check the file, the file 'test.conf' has not been altered! 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.

I have also tried the 'shelljs' module with no luck there. I have tried countless combinations for hours now with no prevail! I'm puzzled.

like image 458
RCG Avatar asked Nov 30 '25 02:11

RCG


1 Answers

I think you need to escape your \n as \\n. Right now, it's getting parsed as a literal newline in the command, which is messing it up.

like image 61
Scimonster Avatar answered Dec 02 '25 16:12

Scimonster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!