Store mysql query output into a shell variable

前端 未结 11 1330
暗喜
暗喜 2020-12-01 06:14

I need a variable to hold results retrieved from the database. So far this is basically what I\'m trying with no success.

myvariable=$(mysql database -u $use         


        
11条回答
  •  清歌不尽
    2020-12-01 06:44

    To read the data line-by-line into a Bash array you can do this:

    while read -a row
    do
        echo "..${row[0]}..${row[1]}..${row[2]}.."
    done < <(echo "SELECT A, B, C FROM table_a" | mysql database -u $user -p $password)
    

    Or into individual variables:

    while read a b c
    do
        echo "..${a}..${b}..${c}.."
    done < <(echo "SELECT A, B, C FROM table_a" | mysql database -u $user -p $password)
    

提交回复
热议问题