简体   繁体   English

Bash脚本循环通过MySQL

[英]Bash Script Loop Through MySQL

I need a bash script that can retrieve MySQL data from a remote data base. 我需要一个可以从远程数据库中检索MySQL数据的bash脚本。 Actually I have that done, but what I need it to do now is loop through the records somehow and pass a variable to another bash file. 实际上我已经完成了,但我现在需要做的是以某种方式遍历记录并将变量传递给另一个bash文件。 Here's my MySQL call: 这是我的MySQL调用:

mysql -X -u $MyUSER -p$MyPASS -h$MyHOST -D$MyDB -e'SELECT `theme_name`, `guid` FROM `themes` WHERE `theme_purchased`="1" AND `theme_compiled`='0';' > themes.xml
download_themes.sh

It exports the data into an xml file called theme.xml right now, I was just trying to figure out some way to loop through the data. 它现在将数据导出到一个名为theme.xml的xml文件中,我只想弄清楚循环数据的方法。 I am trying to avoid PHP and perl and just trying to use bash. 我试图避免PHP和perl,只是尝试使用bash。 Thanks in advance. 提前致谢。

something like: 就像是:

mysql -e "SELECT `theme_name`, `guid` FROM `themes` WHERE `theme_purchased`='1' AND `theme_compiled`='0'" | while read theme_name guid; do
    # use $theme_name and $guid variables
    echo "theme: $theme_name, guid: $guid"
done

in short: the mysql command outputs record separated by '\\n' and fields separated by '\\t' when the output is a pipe. 简而言之:当输出是管道时, mysql命令输出由'\\ n'分隔的记录和由'\\ t'分隔的字段。 the read command reads a line, splits in fields, and puts each on a variable. read命令读取一行,在字段中拆分,并将每个放在一个变量上。

if your data has spaces in the fields, you get some problems with the default read splitting. 如果您的数据在字段中有空格,则会出现默认read拆分的问题。 there are some ways around it; 有一些方法可以解决它; but if you're only reading two fields and one of them shouldn't have any spaces (like the guid ), then you can put the 'dangerous' field at the end, and read will put everything 'extra' in the last variable. 但是如果你只读两个字段而其中一个字段不应该有任何空格(比如guid ),那么你可以将'dangerous'字段放在最后, read会把所有''extra'放在最后一个变量中。

like this: 像这样:

mysql -e "SELECT `guid` `theme_name`, FROM `themes` WHERE `theme_purchased`='1' AND `theme_compiled`='0'" | while read guid theme_name; do
    # use $theme_name and $guid variables
    echo "theme: $theme_name, guid: $guid"
done

Rather than outputting XML, may I suggest you simply use the SELECT INTO OUTFILE syntax or mysql --batch --raw to output tab-separated values. 我建议您只使用SELECT INTO OUTFILE语法或mysql --batch --raw输出制表符分隔值,而不是输出XML。 You then have much easier access through bash to the rest of the Unix toolchain, like cut and awk to retrieve the fields you need and reuse them with Bash. 然后,您可以通过bash更轻松地访问Unix工具链的其余部分,例如cutawk以检索您需要的字段并使用Bash重用它们。 No other scripting language is necessary and you needn't mess with XML. 不需要其他脚本语言,您不必乱用XML。

mysql --batch --raw -u $MyUSER -p$MyPASS -h$MyHOST -D$MyDB -e'SELECT `theme_name`, `guid` FROM `themes` WHERE `theme_purchased`="1" AND `theme_compiled`='0';' \
| awk '{print "theme: " $1  " guid: " $2}'

The accepted answer does not work when spaces are in the output. 当输出中有空格时,接受的答案不起作用。 It is an easy fix (IFS=$'\\t' -- Note the $ -- it is weird): 这是一个简单的修复(IFS = $'\\ t' - 请注意$ - 这很奇怪):


>mysql ... -BNr -e "SELECT 'funny man', 'wonderful' UNION SELECT 'no_space', 'I love spaces';" | while IFS=$'\t' read theme_name guid; do echo "theme: $theme_name guid: $guid"; done
theme: funny man guid: wonderful
theme: no_space guid: I love spaces

You will, of course, want to substitute your own query. 当然,您将要替换自己的查询。

Pipe '|' 管'|' to while is dangerous, for the changes inside the loop happen in another subprocess and would not take effect in the current script. to while是危险的,因为循环内部的更改发生在另一个子进程中,并且不会在当前脚本中生效。

By the way, I hate to turn to the external file solution. 顺便说一句,我讨厌转向外部文件解决方案。

I suggest to use " Process Substitute ". 我建议使用“ Process Substitute ”。

while read field1 field2 field3
do
done < <(mysql -NB -e "$sql")
#     ^
#   space needed

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

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