bash脚本 - 从数据库到变量选择 [英] bash script - select from database into variable

查看:124
本文介绍了bash脚本 - 从数据库到变量选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个变量来保存从数据库中检索的结果。到目前为止,这基本上是我没有成功尝试。

I need a variable to hold results retrieved from the database. So far this is basically what I'm trying with no success.

myvariable=$(mysql database -u $user -p $password | SELECT A, B, C FROM table_a)

我的bash命令的理解是不是因为你可以看到很不错的。

My understanding of bash commands is not very good as you can see.

推荐答案

我不知道很多关于MySQL命令行界面,但假设你只需要与扑帮助,你应该尝试或者命令交换周围像所以:

I don't know much about the MySQL command line interface, but assuming you only need help with the bashing, you should try to either swap the commands around like so:

myvariable=$(echo "SELECT A, B, C FROM table_a" | mysql database -u $user -p$password)

这回声字符串到MySQL。或者,你可以更看中并使用一些新的bash的特性(此处字符串)

which echos the string into MySQL. Or, you can be more fancy and use some new bash-features (the here string)

myvariable=$(mysql database -u $user -p$password<<<"SELECT A, B, C FROM table_a")

造成同样的事情(假设你使用的是最新的bash足够版),不涉及回声。

resulting in the same thing (assuming you're using a recent enough bash version), without involving echo.

请注意,-p $密码是不是一个错字,但是MySQL预计密码通过命令行中输入(与选项和值之间没有空格)的方式。

Please note that the -p$password is not a typo, but is the way MySQL expects passwords to be entered through the command line (with no space between the option and value).

注意MYVARIABLE将包含一切的标准输出(通常是一切,但错误消息)MySQL的输出,包括任何和所有的列标题,ASCII艺术框架等,这可能是也可能不是你想要的是。

Note that myvariable will contain everything that MySQL outputs on standard out (usually everything but error messages), including any and all column headers, ASCII-art frames and so on, which may or may not be what you want.

编辑:结果
正如已经指出的那样,似乎有一个 -e 参数到MySQL,我会去的那一个,肯定。


As has been noted, there appears to be a -e parameter to MySQL, I'd go for that one, definitely.

这篇关于bash脚本 - 从数据库到变量选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆