简体   繁体   English

MySQL命令未从Bash文件运行

[英]MySQL command not running from Bash file

I have the following command 我有以下命令

  mysql -u root -ppass myDB -e "select * from table limit 10;"

which when I run it from a linux server it works normally (10 tuples are displayed) 当我从linux服务器运行它时,它可以正常工作(显示10个元组)

When the following is added in a bash file 在bash文件中添加以下内容时

  echo 'mysql -u root -ppass myDB -e select * from table limit 10;' >> /root/test.log

nothing happens. 什么都没发生。 In the log file, only the echo string is being displayed. 在日志文件中,仅显示回显字符串。 Can someone please help me in this? 有人可以帮我吗?

that is because you just echo your command as a string and redirect the output of echo to the log file. 那是因为你只是echo你的命令字符串和回声的输出重定向到日志文件。 You do nothing else ;-) 你什么都不做;-)

Just run the command as you would do on the command line and redirect the output of mysql command (instead of echo ) to your logfile: 只需像在命令行上那样运行命令,然后将mysql命令的输出(而不是echo )重定向到日志文件:

#!/bin/sh
mysql -u root -ppass myDB -e "select * from table limit 10;" >> /root/test.log

This is because 这是因为

mysql -u root -ppass myDB -e "select * from table limit 10;"

Is not the same as 与...不同

mysql -u root -ppass myDB -e select * from table limit 10;

Try using this: 尝试使用此:

echo 'mysql -u root -ppass myDB -e "select * from table limit 10;"' >> /root/test.log

And then actually invoke the command 然后实际调用命令

mysql -u root -ppass myDB -e "select * from table limit 10;" >> /root/test.log

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM