Select mysql query with bash

How to selectquery mysql with bash so that each column is in a separate array value?

I tried the following command, but it only works if the content is one word. for example: id = 11, text = hello, important = 1

if I have an article, for example in text. the code will not work properly. I think I can use it cut -f -d, but if the "text" contains special characters, it does not work either.

while read -ra line; do
    id=$(echo "${line[1]}")
    text=$(echo "${line[2]}")
    important=$(echo "${line[3]}")

    echo "id: $id"
    echo "text: $text"
    echo "important: $important"

done < <(mysql -e "${selectQ}" -u${user} -p${password} ${database} -h ${host})
+3
source share
1 answer

Bash . , mysql --batch csv .

MySQL:

- , -B

, , . mysql . . raw; . -raw

, , --raw, .

( ), --skip-column-names

. bash IFS ( ).

# myread prevents collapsing of empty fields
myread() {
    local input
    IFS= read -r input || return $?
    while (( $# > 1 )); do
        IFS= read -r "$1" <<< "${input%%[$IFS]*}"
        input="${input#*[$IFS]}"
        shift
    done
    IFS= read -r "$1" <<< "$input"
 }


# loop though the result rows
while IFS=$'\t' myread id name surname url created; do
     echo "id: ${id}";
     echo "name: ${name}";
     echo "surname: ${surname}"; 
     echo "url: ${url}";
     echo "created: ${created}";
done < <(mysql --batch --skip-column-headers -e "SELECT id, name, surname, url, created FROM users")

myread p >

: . echo $row[0] ,

, , char:

row1\t\trow3 [row1,row3] [row1,,row3] , , . .

+3

All Articles