Setting MySQL Query As Multiple Variables In Shell -
i'm sorry if question overly succinct, attempting run mysql query via shell script, , hoping query several different "columns" in result. working well, struggling, figure out how set each of these "columns" unique variables in shell script. here have;
#!/bin/sh results=$(mysql --host=localhost --user=root --password=root database --skip-column-names -e 'select cost1, cost2, cost3 customers';) somemath () { total=$1+$2+$3 echo $total } cost in $results; somemath $cost1 $cost2 $cost3 done exit 0
where struggle figure out how feasibly set of "cost1" in query results $cost1, of "cost2" in query $cost2, , of "cost3" in query $cost3. plan feed these variables function perform other tasks.
i know may rudimentary, despite searching, can't figure out! thank you!
if understand correctly, need each row, each column , pass them other functions separately. here's example wrote:
#!/bin/bash somelistreceiver () { ## know how many values pass can use $1 $2... ## simple math bc echo "$1+$2+$3" | bc } somearrayreceiver () { ## looping received array if don't know how many columns you'll declare -a arrayarguments=("${!1}") c=0 in "${arrayarguments[@]}" let c+=$i done echo $c } while read line ## ifs seperator, can choose is, or can use default space , concat_ws ifs=$';' column in $line ## assign rows each value array columnsar+=($column) ## can check every column ## echo "$column of $line" done ## pass row list function somelistreceiver ${columnsar[@]} ## or pass array function somearrayreceiver columnsar[@] ## unset array refill unset columnsar done < <(mysql --host=localhost --user=root --password=123 test --skip-column-names -e "select concat_ws(';', cost1, cost2, cost3) customers;")
you can change passing array data like.
Comments
Post a Comment