LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Get output from 2 lines and create a variable for bash scripting (https://www.linuxquestions.org/questions/programming-9/get-output-from-2-lines-and-create-a-variable-for-bash-scripting-4175652400/)

bmxakias 04-18-2019 06:28 PM

Get output from 2 lines and create a variable for bash scripting
 
Hello

When i am running a command i am getting as an output:

Code:

appId: f2cn26bf033c3afx
key: 5d54478c8a29a20aa8a2df1718cda545

How can i get them as separated variables so i can use them at a bash script?

Thank you !

rtmistler 04-18-2019 06:49 PM

Pretty sure you can pipe that into awk and use print along with argument numbers to grab each piece, $1 $2

ondoho 04-19-2019 01:52 AM

so essentially, you want bash to eval this:
Code:

appId="f2cn26bf033c3afx"
key="5d54478c8a29a20aa8a2df1718cda545"

???
Code:

help eval
PS: eval is potentially harmful.

grail 04-19-2019 06:26 AM

Please show what you have tried and / or what you are familiar with?

bmxakias 04-19-2019 10:23 AM

I will check the @rtmistler recommendation....

Thank you

szboardstretcher 04-19-2019 11:18 AM

reader.sh < key-value.txt

Code:

# reader.sh
while IFS=": " read -r k v; do
 case "$k" in
  "appId") appId="$v" ;;
  "key") key="$v" ;;
 esac
done

echo "appId = $appId"
echo "key = $key"

Instead of echoing the key/values you could add some logic to act on each pair that it finds. Not sure what you wanted to do, but this might be a good place to start.

bmxakias 04-19-2019 02:01 PM

It works thank you !

grail 04-20-2019 05:44 AM

Please clarify your "it" and show the working solution for others to learn from :)


All times are GMT -5. The time now is 02:56 PM.