Overview
Various scripts below for getting results into a variable, Tab formatted
myvariable=$(echo "SELECT * FROM NIC" | mysql -s --database=opil -u"$user" -p"$pass" -h"$host")
-s makes results Tab delimited
result=mysql -s -u "$user" -p"$pass" -h"$host" <<EOF use $db; $sql; EOF
Unable to keep formatting preserved when results were added to the variable.
To keep formatting make sure the entire command when placed into the variable is surrounded with double quotes:
root@HONE-SERVER:/tank/myscripts # result="$(echo "SELECT * FROM NIC;" | mysql -s --database=opil -uopilAdminExt -pJa??a847Hu?? -h192.168.1.152)" | awk "{ print $A }"
Then when reading the variable, make sure the variable is surrounded in double quotes:
root@HONE-SERVER:/tank/myscripts # echo "$result"
1 OPIL NULL 1000 4 58:9c:fc:10:f8:ad virtio-net 192.168.1.161
1 OPIL NULL NULL 20000 3 TEST MAC ADDRESS XXX 192.168.10.20
Include columns:
root@HONE-SERVER:/tank/myscripts # result="$(echo "SELECT * FROM NIC" | mysql --database=opil -uopilAdminExt -pJa??a847Hu?? -h192.168.1.152 --column-names=TRUE)"
Good link here for parsing a CSV using /bin/sh:
https://www.baeldung.com/linux/csv-parsing