【问题标题】:How to use second argument (input) of system() function如何使用 system() 函数的第二个参数(输入)
【发布时间】:2013-03-22 20:49:40
【问题描述】:

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

当给出 {input} 时,这个字符串被写入一个文件并作为标准输入传递给命令。

我的理解是,如果你的system 电话看起来像这样:

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

执行的命令如下所示:

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

some/temp/file 将文本here is some text 作为其内容。为了测试这一点,我运行了 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} 参数,我缺少什么?为什么它似乎适用于 cat,但不适用于 echo 或我自己的脚本?

【问题讨论】:

  • 嘿,投反对票的人。介意告诉我如何改进我的问题吗?

标签: vim vim-plugin


【解决方案1】:

你错了;执行的命令是不是

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

而是

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

或更好

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

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

【讨论】:

    猜你喜欢
    • 2017-12-12
    • 2019-12-09
    • 2013-11-17
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多