Hi guys i wrote a bash script that execute a query e and print the result into a file but it doesn't work
大家好我写了一个bash脚本执行查询e并将结果打印到文件但它不起作用
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
进入db.config有访问db的凭据
3 个解决方案
#1
1
You don't need to reinvent the wheel to get mysql login credentials. Use mysql_config_editor to store your credentials encrypted in ~/.mylogin.cnf, then you can do the whole thing in a simple one-liner e.g.
您不需要重新发明*来获取mysql登录凭据。使用mysql_config_editor存储在〜/ .mylogin.cnf中加密的凭据,然后你可以用一个简单的单行代码来完成整个过程。
mysql -s -B -q -e "select * from information_schema.columns" | tee ./tmpsql/columns.txt
#2
1
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
#3
1
Don't put command in variables as [ this ] answer nicely summarizes.
不要把命令放在变量中,因为[this]回答很好地总结了。
A [ heredoc ] is your friend here.
[heredoc]是你的朋友。
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_USER
for user defined variables as it may conflict with built-in shell variables. Double quoting variables is also a standard practice.
注意:不要对用户定义的变量使用完整的大写标识符,如$ DB_USER,因为它可能与内置的shell变量冲突。双引号变量也是一种标准做法。
#1
1
You don't need to reinvent the wheel to get mysql login credentials. Use mysql_config_editor to store your credentials encrypted in ~/.mylogin.cnf, then you can do the whole thing in a simple one-liner e.g.
您不需要重新发明*来获取mysql登录凭据。使用mysql_config_editor存储在〜/ .mylogin.cnf中加密的凭据,然后你可以用一个简单的单行代码来完成整个过程。
mysql -s -B -q -e "select * from information_schema.columns" | tee ./tmpsql/columns.txt
#2
1
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
#3
1
Don't put command in variables as [ this ] answer nicely summarizes.
不要把命令放在变量中,因为[this]回答很好地总结了。
A [ heredoc ] is your friend here.
[heredoc]是你的朋友。
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_USER
for user defined variables as it may conflict with built-in shell variables. Double quoting variables is also a standard practice.
注意:不要对用户定义的变量使用完整的大写标识符,如$ DB_USER,因为它可能与内置的shell变量冲突。双引号变量也是一种标准做法。