0

I am trying to store each value from the following sql select statement and store them in separate variables using bash.

 #!/bin/bash
 mysqlhost="thehost"
 mysqldb="thedb"
 mysqlun="theusername"
 mysqlpw="thepassword"
 mysqlconnection="--disable-column-names --host=$mysqlhost --user $mysqlun --password=$mysqlpw --database=$mysqldb"

declare -a pinIDs=$(mysql $mysqlconnection -e "SELECT pinID FROM somewhere WHERE something = something";)

I get the following result when I use code

echo $pinIDs    

8 11 23 26

I need to store each of those values into their own variable.

2 Answers 2

1

Add brackets to put output in array pinIDs. Replace

declare -a pinIDs=$(mysql $mysqlconnection -e "SELECT pinID FROM somewhere WHERE something = something";)

by

declare -a pinIDs=( $(mysql $mysqlconnection -e "SELECT pinID FROM somewhere WHERE something = something";) )

Then see output of: declare -p pinIDs

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

Comments

0

For others that are using this to learn. What I did after fixing the brackets in order to put the result into an array such as (8 11 23 26) instead of 8 11 23 26, was this:

cnt=${#pinIDs[@]}
for (( i=0 ; i<cnt ; i++ ))
do
echo "pinId: ""${pinIDs[$i]}"
done

Comments

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.