0

I have two .jar file in a pipeline. 1.jar will output two lines, both of which will be the input of 2.jar. Now I want to store the each line of the intermediate output of 1.jar to variables A and B, while allowing 2.jar to take both lines as input.

java -jar 1.jar | XXX | java -jar 2.jar

As a detour, I could do

java -jar 1.jar | tee out | java -jar 2.jar

and read the file to save the variables, but I'd like a more straight-forward way of doing it.

1 Answer 1

1

The final version, with refinements by Jonathan Leffler:

IFS=$'\n' a=($(java -jar 1.jar)); printf "%s\n" "${a[@]}" | java -jar 2.jar
echo ${a[0]}   # line 1
echo ${a[1]}   # line 2
Sign up to request clarification or add additional context in comments.

5 Comments

You probably need echo "$a" so that the newline is preserved. Consider: a=($(java -jar 1.jar)); printf "%s\n" "${a[@]}" | java -jar 2.jar. This gives you the two lines in two separate array elements. I was working around to an array based solution, but hadn't got there yet.
Don't downplay your contribution; I was playing stupid games with 3>&1 etc (and failing, I might add), but you simplified the process by capturing the output and then feeding it. All I did was help a little with refining that important idea!
@JonathanLeffler Cool Team effort then.
Thanks for the suggestion. From my side ${a[@]} seems to store each token instead of each line. Instead of "token11 token12 token13\ntoken21 token22\n", "%s\n" "${a[@]}" is printing "token11\ntoken12\ntoken13\ntoken21\ntoken22\n"
@user2684206 Good point! (I tested it with a single word per line so I didn't notice that.) I believe that setting the internal field separator to newline will solve the problem. Check out the edit above.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.