Node.js 和 child_process.exec 参数的问题
在命令行上,如果我运行
echo -n "foo" | openssl aes-128-cbc -k "key" -base64 -e
(-n
标志阻止 echo
在其输出末尾添加换行符),我会得到
U2FsdGVkX1+nMW5I4eZSasPKfsUuCpbFsnn56ngEdec=
但是当我运行
exec = require('child_process').exec;
exec('echo -n "foo" | openssl aes-128-cbc -k "key" -base64 -e', callback);
回调 时输出
U2FsdGVkX1/CARBiGos0x9ALNhFqcIaFvZ9EUvVBxuc=
为什么不同?解密它,你会得到字符串
-n foo
所以不知何故,exec
将-n“foo”
编码为“-n foo”
(在Node下0.4.2)。
这是最奇怪的部分:当我直接从 TextMate 运行代码时(通过 jashkenas 的 CoffeeScript 包),我没有遇到这个问题。起初我以为这是一个路径问题,但事实并非如此(在两个环境中使 PATH
相同没有效果)。也许这是因为一种环境是 TTY,而另一种环境不是。
其他人是否意识到这种不一致?这是一个 Node 错误,还是我忽略了某些东西?我猜如果我使用较低级别的 spawn
而不是 exec
,我的问题就会消失。
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" | openssl 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
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您的
/bin/echo
不尊重-n
?echo
通常是 shell 内置命令,人们可能会尊重-n
。您可能希望使用printf(1)
来代替,它更便携。Perhaps your
/bin/echo
doesn't respect-n
?echo
is frequently a shell builtin, and that one may respect-n
. You may wish to useprintf(1)
instead, it is more portable.