Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Why can't I remove new line characters from being added with Node's exec function?

Why can't I remove new line characters from being added with Node's exec function?

Problem

I have the following to execute shell commands using Node:

function puts( error, stdout, stderr ) {
    stdout = sanitize( stdout ).rtrim("\n"); // remove new line
    sys.puts( stdout );
}

I use this library to try and Remove new lines:

npm install validator

but when I do something like this:

exec( "ls -l", puts );

it puts a new line character on the end.

I'm using Linux so the new line should just be a \n right?!

How do I stop the new line from happening?

Many thanks.

Problem courtesy of: ale

Solution

Are you sure that the call to sys.puts isn't what's adding the newline? Try sys.print and see what you get.

In other news, I think that sys.puts (and sys in general) have been deprecated in node.js for a while. You should probably use console.log for this kind of thing.

Solution courtesy of: Cameron

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Why can't I remove new line characters from being added with Node's exec function?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×