How to run bash command during redirect << EOF

我的未来我决定 提交于 2019-12-10 20:38:08

问题


I know there's some way to run bash command during redirect, but I don't know how exactly it is done.

I want to do something like this:

#!/bin/bash
mysql -uUser -pPasswd << EOF
echo 'I wanna echo something by using echo which is run by bash'
use Mydb
some sql commands here
commit;
EOF

I had ever done that by mistake by using " in the "<< EOF" , but failed to make it now.


回答1:


You can use system command from within the mysql command line client:

#!/bin/bash
mysql -uUser -pPasswd << EOF
system echo 'I wanna echo something by using echo which is run by bash';
use Mydb
some sql commands here
commit;
EOF



回答2:


In addition to using a heredoc to interact with MySQL from bash, you can simply call mysql itself in batch mode. This is useful when you have reasonably short queries and will eliminate needing to call system from within the heredoc body. The general form in bash is:

$(mysql -uuser -hhost database.table -Bse "any valid mysql command")

(you may omit .table if it is identified in your query) To handle the information returned by mysql, it is usually advisable to return the information to an array:

results=( $(mysql -uuser -hhost database.table -Bse "any valid mysql command") )

In that regard, you can structure your scripts in a more flexible manner.



来源:https://stackoverflow.com/questions/24473216/how-to-run-bash-command-during-redirect-eof

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!