0

My JAVA code output looks like below.

    Name="John"
    Age="23"
    Gender="Male"
    Phone="+123 456 789"
    ..
    ..

I want to use above java code output as variables within the shell-script without writing any intermediate file. I'm trying to get the variables using below command inside Myscript.sh

JAVA_OUTPUT=`java -cp ojdbc6.jar:. javaClassFile args`
echo $JAVA_OUTPUT

But I'm unable to set the java output as variables within shell-script

1 Answer 1

2

You can use an associative array to store all the key/value pairs:

declare -A vars
while IFS='=' read -r key value; do
    vars[$key]=$value;
done < <(java -cp ojdbc6.jar:. javaClassFile args)
echo "${vars[Name]} is ${vars[Age]}"
# Etc.

Or if you change your java program to add quotes around the values with spaces like the phone number one, something like source <(java -cp ojdbc6.jar:. javaClassFile args) might work for creating multiple variables.

Sign up to request clarification or add additional context in comments.

4 Comments

I have modified my java code to add quotes for values but source command is not working for declaring variables. And while loop throwing syntax error near unexpected token `<'
@balachandar Sure you're using bash?
Yes. I'm using bash env
@balachandar source <(your program) has to work in Bash – according to Wikipedia, the functionality behind the syntax has been there since at least 1994. Make sure you don't add any extra space character between the "less than" sign and the left parenthesis.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.