如何使用 system() 函数的第二个参数(输入) [英] How to use second argument (input) of system() function

查看:17
本文介绍了如何使用 system() 函数的第二个参数(输入)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The documentation to vim's system function says this about the second argument:

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

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

我从中了解到,如果您的 system 调用看起来像这样:

What I understood from that was that if your system call looked like this:

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 命令(第二行是结果):

and some/temp/file would have the text here is some text as its contents. To test this, I ran the vim command (the second line is the result):

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

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

Ok, that looks right. A second test:

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

我没有得到一些临时文件的名称,而是得到了一个空行.此外,当我在 node.js 脚本中打印 process.argv 时,我只得到 ['node', 'path/to/something.js', '--file'].

Instead of getting some temporary file's name, I got a blank line. Moreover, when I print process.argv in my node.js script, I just get ['node', 'path/to/something.js', '--file'].

关于如何使用 {input} 参数,我遗漏了什么?为什么它似乎适用于 cat,而不适用于 echo 或我自己的脚本?

What am I missing about how the {input} argument is to be used? How come it seems to work for cat, but not echo or my own script?

推荐答案

你错了;执行的命令不是

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

而是

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

或更好

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

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

If you want text passed as arguments, just append this to the first argument of system() (properly escaped through shellescape()).

这篇关于如何使用 system() 函数的第二个参数(输入)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆