简体   繁体   English

如何在 jrunscript 或 jjs 中访问 stdin(在 win7 上)

[英]how to access stdin in jrunscript or jjs (on win7)

i have googled around quite a bit but couldnt find anything that tells me how to get access to the stdin when executing a JS with jrunscript.我在谷歌上搜索了很多,但找不到任何告诉我在使用 jrunscript 执行 JS 时如何访问标准输入的信息。

it seems not to be possible.这似乎是不可能的。

use case: i want to write a little JS script that does a regex-replace for use on the cmd where i would want to pass in text data via piping, eg用例:我想编写一个小的 JS 脚本,它执行正则表达式替换以在 cmd 上使用,我想通过管道传递文本数据,例如

>cat file | jrunsscript -f apply-regex.js

... and out comes the result (which i could pipe into a file, etc). ...结果出来了(我可以将其导入文件等)。

hence, i need to access to the stdin in order to read the what's piped unto jrunsscript.因此,我需要访问 stdin 才能读取通过管道传输到 jrunsscript 的内容。

side note: for this i usually use sed, but there are problems when the regex itself involves quotes in certain cases.旁注:为此,我通常使用 sed,但是当正则表达式本身在某些情况下涉及引号时会出现问题。

Not sure if this is on topic: http://docs.oracle.com/javase/7/docs/technotes/tools/share/jsdocs/GLOBALS.html不确定这是否是主题: http : //docs.oracle.com/javase/7/docs/technotes/tools/share/jsdocs/GLOBALS.html

read(prompt, multiline) Reads one or more lines from stdin after printing a prompt read(prompt, multiline)在打印提示后从 stdin 中读取一行或多行

Based on your comment, I think you need to use arguments as shown: https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jrunscript.html根据您的评论,我认为您需要使用如下所示的参数: https : //docs.oracle.com/javase/8/docs/technotes/tools/unix/jrunscript.html

Usage:用法:

jrunscript -f test.js "Hello World"

Get the argument:获取参数:

document.write(arguments[0]);

Upon more research, you may be able to read via Interactive mode:经过更多研究,您可以通过交互模式阅读:

jrunscript -f -

Interactive mode, read script from standard input.交互模式,从标准输入读取脚本。 If this is used, this should be the last -f option如果使用它,这应该是最后一个 -f 选项

But still not clear if this is what you're looking for exactly.但仍然不清楚这是否正是您要寻找的。

the workaround thus far is to到目前为止的解决方法是

  1. pipe the input into a file将输入通过管道传输到文件中
  2. pass tempfile as arg to jsrunscript将临时文件作为 arg 传递给 jsrunscript
  3. use JS output as needed根据需要使用 JS 输出

Take a dip in Java.在 Java 中畅游。

in file JJS_SAYS_HI.js在文件JJS_SAYS_HI.js

//Java-y bit
var br = new java.io.BufferedReader(
    new java.io.InputStreamReader(
            java.lang.System.in,
            java.nio.charset.StandardCharsets.UTF_8)
    );
//JavaScript-y bit
var line = "";
while(br.ready()) {
    line = br.readLine();
    print("JJS says: ${line}");
};

Test the whole mess with a command like: echo Hi! | jjs -scripting JJS_SAYS_HI.js使用如下命令测试整个混乱: echo Hi! | jjs -scripting JJS_SAYS_HI.js echo Hi! | jjs -scripting JJS_SAYS_HI.js

Output is JJS says: Hi!输出是JJS says: Hi! (on Windows) (在 Windows 上)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM