Zak
Zak

Reputation: 7515

MySQL query splitting unexpectedly in Bash Script

I am having issues and not understanding what the underlying cause is.

I have a table that has these three fields:

______________________________________________
|   cid    |    order_id    |    TxRefNum    |
----------------------------------------------

I am making a simple call in my bash script (there is literally no other code to start with)

#!/bin/bash

mysql --login-path=main-data -e "SELECT 
    cid,
    order_id,
    TxRefNum 
FROM database.orders_temp" |

while read this that other; do

    echo "$this  ||  $that  ||  $other"
done

I would expect to see the following:

__________________________________________________________
|    29    |    F0VIc - CHATEAU ROOFIN    |   5555555    |
----------------------------------------------------------

Instead my script is splitting the string $that into two different strings .. The echo is actually:

___________________________________________________
|    29    |    F0VIc    |    - CHATEAU ROOFIN    |
---------------------------------------------------

Do I have to set a delimiter when setting my variables in my while loop?? I am truly stumped!!

Upvotes: 1

Views: 313

Answers (1)

cwallenpoole
cwallenpoole

Reputation: 82028

Getting output from the mysql command formatted in an intelligent way is problematic. In your case bash is interpreting the as a delimiter. You need to split a different way. I was able to get this working. You'll note the | in the query as well at the IFS line at the tope

#!/bin/bash

IFS='|' # set the delimiter
mysql --login-path=main-data -e "SELECT
    29 as cid, '|',
    'F0VIc - CHATEAU ROOFIN' as order_id,
    '|',
    5555555 as TxRefNum
FROM dual" |
while read this that other; do
    echo "$this  ||  $that  ||  $other"
done

Upvotes: 1

Related Questions