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
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 USE INFORMATION_SCHEMA; SELECT * FROM COLUMNS; EOF
All good :-)
Note: Don't use full uppercase identifiers like
$DB_USERfor user defined variables as it may conflict with built-in shell variables. Double quoting variables is also a standard practice.
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