Processing MySQL result in bash

后端 未结 3 361
抹茶落季
抹茶落季 2021-01-22 07:05

I\'m currently having a already a bash script with a few thousand lines which sends various queries MySQL to generate applicable output for munin.

Up until now the resul

3条回答
  •  自闭症患者
    2021-01-22 07:24

    You would use a while read loop to process the output of that command.

    echo "SELECT id, name FROM type ORDER BY sort" | mysql test | while read -r line
    do
        # you could use an if statement to skip the header line
        do_something "$line"
    done
    

    or store it in an array:

    while read -r line
    do
        array+=("$line")
    done < <(echo "SELECT id, name FROM type ORDER BY sort" | mysql test)
    

    That's a general overview of the technique. If you have more specific questions post them separately or if they're very simple post them in a comment or as an edit to your original question.

提交回复
热议问题