Bash script and lines

My problem is with mysql_query strings. I need:

Record 0: 2,text is text,3.23

      

But I have:

Record 0: 2
Record 1: text
Record 2: is
Record 3: text
Record 4: 3.23

      

Please help me.

results=($(mysql --user root -proot test -Bse "select id,name from Object"));

cnt=${#results[@]}

for (( i=0 ; i<${cnt} ; i++ ))
do
    echo "Record No. $i: ${results[$i]}"

    fieldA=${results[0]};
    fieldB=${results[1]};

done

      

+3


source to share


1 answer


The problem is you are storing the output mysql

to an array. Now, if it mysql

returns multiple records, you won't know when the record ends, but the next one will start, because the array will contain "flattened" data, for example.( record1_fieldA record1_fieldB record2_fieldA record2_fieldB ... )

Instead, use a loop while

to iterate over records like this:



i=0
while read fieldA fieldB
do
    echo "Record $(( i++ )): fieldA: $fieldA fieldB: $fieldB"
done < <(mysql --user root -proot test -Bse "select id,name from Object")

      

+6


source







All Articles