如何向通过 jconsole.exe 运行的 J 脚本提供 STDIN 数据?

Dan*_*ane 3 j

我想运行一个 J 脚本,提供 STDIN,并使用 STDOUT 接收脚本的输出。

我觉得我错过了一些非常明显的东西,但是使用 jconsole.exe帮助页面是 . . . 简洁。

我天真的想法是我可以在 cmd.exe shell 中运行以下命令来提供 STDIN:

jconsole.exe script.ijs inputstring
Run Code Online (Sandbox Code Playgroud)

虽然这在没有尝试 STDIN 的情况下有效:

C:\..\bin>jconsole.exe "C:\path\no-input-script.ijs"
success
C:\..\bin>
Run Code Online (Sandbox Code Playgroud)

no-input-script.ijs文件如下:

stdout 'success'
exit ''
Run Code Online (Sandbox Code Playgroud)

我有以下script-with-input.ijs文件:

input =: stdin ''
stdout 'input was ' , input
exit ''
Run Code Online (Sandbox Code Playgroud)

当我运行以下命令时,系统挂起:

C:\..\bin>jconsole.exe "C:\path\script-with-input.ijs" xyz
Run Code Online (Sandbox Code Playgroud)

当我然后点击Ctrl+ 时C,脚本退出,我留下以下内容:

C:\..\bin>jconsole.exe "C:\path\script-with-input.ijs" xyz
input was
C:\..\bin>
Run Code Online (Sandbox Code Playgroud)

Eel*_*vex 5

stdin从 STDIN 读取输入直到 EOF(通常在 *nix ^D 中)。所以你的 'script-with-input.ijs' 等待用户输入或管道。

c:>jconsole.exe "script-with-input.ijs" hello
this is user input
^D
input was this is user input
Run Code Online (Sandbox Code Playgroud)

相反,您要做的是读取命令的参数。这些存储在 ARGV 中:

NB. script-with-input.ijs
input =: ARGV
echo input
exit''
Run Code Online (Sandbox Code Playgroud)

然后:

c:>jconsole.exe "script-with-input.ijs" hello
??????????????????????????????????????????
?jconsole.exe?script-with-input.ijs?hello?
??????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)

  • 我无法在 Windows 上验证这一点,但以下应该有效: 1. 重定向输入 `jconsole script.ijs < input.txt` 2. 管道命令 `echo "hello" | jsconsole script.ijs` 3. 管道文本文件`type input.txt | jsconsole script.ijs`。 (2认同)