Problem with Node.js and child_process.exec arguments
On the command line, if I run
echo -n "foo" | openssl aes-128-cbc -k "key" -base64 -e
(the -n
flag prevents echo
from adding a newline to the end of its output), I get
U2FsdGVkX1+nMW5I4eZSasPKfsUuCpbFsnn56ngEdec=
But when I run
exec = require('child_process').exec;
exec('echo -n "foo" | o开发者_如何转开发penssl aes-128-cbc -k "key" -base64 -e', callback);
the callback gets the output
U2FsdGVkX1/CARBiGos0x9ALNhFqcIaFvZ9EUvVBxuc=
Why is it different? Decrypt it, and you'll get the string
-n foo
So somehow, exec
encoded -n "foo"
into "-n foo"
(under Node 0.4.2).
Here's the weirdest part: I don't get this problem when I run my code directly from TextMate (via jashkenas' CoffeeScript bundle). At first I thought it was a path issue, but it isn't (making PATH
identical in the two environments had no effect). Perhaps it's because one environment is a TTY and one isn't.
Are other folks aware of this inconsistency? Is this a Node bug, or am I ignoring something? I'm guessing that my problems will go away if I use the lower-level spawn
instead of exec
.
Perhaps your /bin/echo
doesn't respect -n
? echo
is frequently a shell builtin, and that one may respect -n
. You may wish to use printf(1)
instead, it is more portable.
精彩评论