问题
The documentation to vim's system
function says this about the second argument:
When {input} is given, this string is written to a file and passed as stdin to the command.
What I understood from that was that if your system
call looked like this:
call system('node something.js --file', 'here is some text')
The command executed would look like this:
node something.js --file some/temp/file
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>
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']
.
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?
回答1:
You've got it wrong; the command executed is not
node something.js --file some/temp/file
but rather
echo "some/temp/file" | node something.js --file
or better
node something.js --file < some/temp/file
If you want text passed as arguments, just append this to the first argument of system()
(properly escaped through shellescape()
).
来源:https://stackoverflow.com/questions/15579613/how-to-use-second-argument-input-of-system-function