localhost
localhost

Reputation: 1273

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:

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

cat inputfile | whatever

, which would perform exactly the same 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?

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.

Upvotes: 2

Views: 2015

Answers (3)

Thomas Blankenhorn
Thomas Blankenhorn

Reputation: 256

Solution:

#!/bin/bash
exec java -jar whatever.jar "$@"

Discussion:

Like all Unix shells, Bash has a special variable named $@. It contains the argument list of a shell script. I would use it to receive the <args> of whatever.jar.

This way, Bash never even touches the stdin and stdout of whatever.jar. It simply has java call whatever.jar, passes <args> to it by way of $@, and gets out of the way. All reading from stdin and writing to stdout gets done by whatever.jar directly.

Upvotes: 5

Reto H&#246;hener
Reto H&#246;hener

Reputation: 5808

I think your bash script should only contain the java call.

Then redirect the file as input to the script:

./script < inputfile

Upvotes: -1

Cyrus
Cyrus

Reputation: 88684

This should do the job:

#!/bin/bash
java -jar whatever.jar <args>

Upvotes: 0

Related Questions