[英]How to make bash wrapper script to execute java jar that takes stdin and outputs to stdout
I've made a java program that takes input from stdin and returns output to stdout, which runs perfectly fine using:我制作了一个 java 程序,该程序从标准输入获取输入并将 output 返回到标准输出,使用以下命令运行得非常好:
cat inputfile | java -jar whatever.jar <args>
I want to make a bash wrapper script to put in my ~/bin
so I can just run我想制作一个 bash 包装脚本放入我的
~/bin
中,这样我就可以运行
cat inputfile | whatever
, which would perform exactly the same function. ,这将执行完全相同的 function。
How can I make a wrapper script that just passes stdin unmolested to the jar, while simultaneously receiving stdout back to echo to the CLI?如何制作一个包装脚本,将标准输入不受干扰地传递给 jar,同时接收标准输出回显到 CLI?
I think I can achieve it one-way to send the inputfile to the command, but have no idea how to get the output from the command back simultaneously.我想我可以通过一种方式将输入文件发送到命令,但不知道如何同时从命令中获取 output。
Solution:解决方案:
#!/bin/bash
exec java -jar whatever.jar "$@"
Discussion:讨论:
Like all Unix shells, Bash has a special variable named $@
.像所有 Unix shell 一样,Bash 有一个名为
$@
的特殊变量。 It contains the argument list of a shell script.它包含 shell 脚本的参数列表。 I would use it to receive the
<args>
of whatever.jar
.我会用它来接收
<args>
的whatever.jar
。
This way, Bash never even touches the stdin
and stdout
of whatever.jar
.这样,
stdin
甚至永远不会触及whatever.jar
的标准输入和stdout
。 It simply has java
call whatever.jar
, passes <args>
to it by way of $@
, and gets out of the way.它只是让
java
调用whatever.jar
,通过$@
将<args>
传递给它,然后让开。 All reading from stdin
and writing to stdout
gets done by whatever.jar
directly.所有从
stdin
读取和写入stdout
都由whatever.jar
直接完成。
This should do the job:这应该做的工作:
#!/bin/bash
java -jar whatever.jar <args>
I think your bash script should only contain the java call.我认为您的 bash 脚本应该只包含 java 调用。
Then redirect the file as input to the script:然后将文件作为输入重定向到脚本:
./script < inputfile
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.