user2684206
user2684206

Reputation: 45

Save pipeline output to a variable

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.

Upvotes: 0

Views: 372

Answers (1)

ooga
ooga

Reputation: 15501

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

Upvotes: 1

Related Questions