微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

vim – 如何使用system()函数的第二个参数(输入)

vim系统函数的文档说明了第二个参数:

When {input} is given,this string is written to a file and passed as stdin to the command.

我从中理解的是,如果您的系统调用如下所示:

call system('node something.js --file','here is some text')

执行的命令如下所示:

node something.js --file some/temp/file

并且一些/ temp /文件将使这里的文本是一些文本作为其内容.为了测试这个,我运行了vim命令(第二行是结果):

:echo system('cat','here is some text')
here is some text

好的,看起来不错.第二次测试:

:echo system('echo','here is some text')
<blank line>

我得到一个空白行,而不是获得一些临时文件名称.而且,当我在node.js脚本中打印process.argv时,我只得到[‘node’,’path / to / something.js’,’ – file’].

关于如何使用{input}参数,我错过了什么?为什么它似乎适用于猫,但不是回声或我自己的脚本?

解决方法

你错了;执行的命令不是

node something.js --file some/temp/file

反而

echo "some/temp/file" | node something.js --file

或更好

node something.js --file < some/temp/file

如果您希望将文本作为参数传递,只需将其附加到system()的第一个参数(通过shellescape()正确转义).

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐