简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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