Get columns name and write it into a file

Hi guys i wrote a bash script that execute a query e and print the result into a file but it doesn't work

source ../db.config
msqlcmd="mysql -u $DB_USER -p$DB_PASS -s -B -q"
echo "USE INFORMATION_SCHEMA;\nSELECT * FROM COLUMNS" | $mysqlcmd > ../tmpsql/columns.txt

Into the db.config there are the credential to access to db

2 answers

  • answered 2018-01-21 10:00 sjsam

    Don't put command in variables as [ this ] answer nicely summarizes.

    A [ heredoc ] is your friend here.

    mysql -u "$db_user" -p "$db_pass" -s -B -q <<-EOF > ../tmpsql/columns.txt

    All good :-)

    Note: Don't use full uppercase identifiers like $DB_USER for user defined variables as it may conflict with built-in shell variables. Double quoting variables is also a standard practice.

  • answered 2018-01-21 10:00 Allan

    try the following code:

    source ../db.config
    echo -e "USE INFORMATION_SCHEMA;\nSELECT * FROM COLUMNS" | mysql -u $DB_USER -p$DB_PASS -s -B -q > ../tmpsql/columns.txt