Bash + MySQL -d反引号问题 [英] Bash + MySQL -d backtick issue
问题描述
我正在按照社区精神为社区项目创建解决方案,以免费监测河流水位.这项工作的最终结果将是一个系统,该系统将从河流水位探测器中获取数据,并为在线社区生成图形.
I am helping to create a solution for a community project to monitor river levels for free, in the community spirit. The eventual product of this effort will be a system that takes data from a river level probe, and generates a graph for an online community.
我很早就开始从事该项目,并且据我所知,Bash正在使用它从包含数据的探针中获取一个文本文件,并将其推送到MySQL数据库中.直到我遇到绊脚石之前,一切都进行得非常顺利.我的许多数据库表列都是数字,因此必须使用MySQL命令行工具反引号与之交互.不幸的是,我想从MySQL语句中获取输出并将其放入变量中,但是我认为由于两组反向标记,MySQL无法获得正确的命令.
I am early on with the project, and as I am familiar with Bash am using this to take a text file from the probe containing the data and push it into a MySQL database. All has been going really nicely until I hit a stumbling block. A number of my database table columns are numbers and so to interact with these using the MySQL command line tool backticks have to be used. Unfortunately I want to take the output from a MySQL statement and put it into a variable, but I think due to the two sets of back ticks MySQL is not getting the right command.
我用一种讨厌的方法来捏造它,就像这样使它工作效率低下:
I have fudged it with a nasty work around like so which works all be it inefficiently:
mysql -N -D $targetDatabase -e "select \`"$timeSample"\` from RiverDataDays where date="$dateOfFile";" >tmpValue
dbEntry=`cat tmpValue`
echo $dbEntry
但是实际上,我要执行的操作是将其直接推入这样的变量中:
But actually, what I want to do is push it straight into an variable like this:
dbEntry=`mysql -N -D $targetDatabase -e "select \`"$timeSample"\` from RiverDataDays where date="$dateOfFile";"`
echo $dbEntry
推荐答案
尝试执行此操作:
dbEntry="$(printf "SELECT \140%s\140 FROM 'RiverDataDays' WHERE date = '%s';\n" "$timeSample" "$(<tmpValue )" | mysql -N -D "$targetDatabase")"
echo "$dbEntry"
或
dbEntry="$(printf "SELECT \`%s\` FROM 'RiverDataDays' WHERE date = '%s';\n" "$timeSample" "$(<tmpValue )" | mysql -N -D "$targetDatabase")"
echo "$dbEntry"
反引号(`)用于旧式命令替换,例如
The backquote (`) is used in the old-style command substitution, e.g.
foo=`command`
The
foo=$(command)
建议使用
语法. $()中的反斜杠处理不足为奇,并且$()更易于嵌套.参见 http://mywiki.wooledge.org/BashFAQ/082
\140
是反引号的八进制表示形式,请参见
is the octal representation of a backtick, see
man ascii
这篇关于Bash + MySQL -d反引号问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!