在 Windows 命令行上伪造标准输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15994824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Faking standard input on the Windows command line
提问by Ryan
I want to use Evernote's ENScript.exeto create new notes, entering the text and title as arguments. The problem is that ENScript only allows the text to be entered via a file or via standard input.
我想使用Evernote 的 ENScript.exe创建新笔记,输入文本和标题作为参数。问题是 ENScript 只允许通过文件或标准输入输入文本。
For my current workaround I use a .bat file to write the argument to a file, then call ENScript with the /s
argument pointing to the file to read it in, but that forces the default title to the temporary file's filename (a behavior I do not want).
对于我当前的解决方法,我使用 .bat 文件将参数写入文件,然后使用/s
指向文件的参数调用 ENScript以读取它,但这会强制将默认标题设为临时文件的文件名(我不这样做的行为)想)。
So my question is: Is there a way to "fake" standard input on the Windows command line so that I can use an argument (passed from another program) to generate the note text? The beginnings of the script would be something like
所以我的问题是:有没有办法在 Windows 命令行上“伪造”标准输入,以便我可以使用参数(从另一个程序传递)来生成注释文本?脚本的开头将类似于
ENScript.exe createNote /i %1
with the standard input following.
标准输入如下。
回答by dbenham
You are looking for a pipe operation that captures the output of one command and sends it as input to the next. This is a standard capability in most operating systems.
您正在寻找一种管道操作,它捕获一个命令的输出并将其作为输入发送到下一个命令。这是大多数操作系统中的标准功能。
The pipe symbol for Windows CMD is |
Windows CMD 的管道符号是 |
Your script could be as simple as:
您的脚本可能很简单:
@echo %~2|@ENScript.exe createNote /i %1
If your script is called MakeNote.bat, you would call it like
如果您的脚本名为 MakeNote.bat,您可以这样称呼它
MakeNote "your title" "This is the text of your note"
回答by michaelb958--GoFundMonica
One can "fake" standard input by using redirection:
可以通过使用重定向来“伪造”标准输入:
command args... < filename args...
where the <
means input redirection ("read standard input from the filename after the <
instead of the terminal").
其中<
意味着输入重定向(“从而<
不是终端之后的文件名读取标准输入”)。
(Note that old Windows or DOS programs may read straight from the terminal, making input redirection useless; this hopefully won't apply to something as recent as Evernote.)
(请注意,旧的 Windows 或 DOS 程序可能会直接从终端读取,从而使输入重定向无用;这希望不适用于像 Evernote 这样的最新版本。)
For your example:
对于您的示例:
ENScript.exe < "%1"
Feel free to add more arguments before or after the redirection. For example, if your script will be called as script filename title
, you will want to invoke ENScript /i "%2" < "%1"
.
在重定向之前或之后随意添加更多参数。例如,如果您的脚本将被称为 as script filename title
,您将需要调用ENScript /i "%2" < "%1"
.
回答by Zimba
echo Note text>Title&ENScript.exe createNote /s Title