我正在使用此命令:
mysql -u user -ppassword database -e "select distinct entityName,entitySource from AccessControl"
输出如下:
+-----------------------+--------------+
| entityName | entitySource |
+-----------------------+--------------+
| low | Native |
| high | Native |
| All Groups | AD |
| Help Ser vices Group | AD |
| DEFAULT_USER_GROUP | Native |
| SYSTEM | Native |
| DEFAULT_BA_USER_GROUP | Native |
| soUsersGrp | Native |
+-----------------------+--------------+
我的问题是:如何动态创建变量数组来存储值entityName
和entitySource
?我需要使用的是使用entityName和entitySource的每个值来更新另一个表。
早些时候我试图将输出存储在一个文件中并使用awk访问每一行,但这没有用,因为一行可能包含多个单词。
答案 0 :(得分:1)
当然,这可以做到。我想说明在shell中将mysql管道化为mysql的想法很尴尬,但我理解为什么可能需要这样做(例如在将mysql
传递给psql
或其他什么时)。< / p>
mysql -qrsNB -u user -p password database \
-e "select distinct entityName,entitySource from AccessControl" | \
while read record; do
NAME="`echo $record|cut -d' ' -f 1`" # that's a tab delimiter
SOURCE="`echo $record|cut -d' ' -f 2`" # also a tab delimiter
# your command with $NAME and $SOURCE goes here ...
COMMAND="select trousers from namesforpants where entityName='${NAME}'" # ...
echo $COMMAND | mysql # flags ...
done
-rs
标记修剪你的输出,这样你就不必知道它给你的那个表,-q
要求结果不被缓冲,-B
要求批处理模式,-N
要求没有列名。
您对这些变量的处理取决于您;可能我会在那个循环中编写语句并将它们提供给后续进程,而不是担心插值和引号,因为你已经提到过你的一些数据中有空格。或者,您可以编写/附加到文件,然后将其提供给后续流程。
像往常一样manual is your friend。我也将成为你的朋友,但是联机帮助页就是这些东西的答案。 : - )
答案 1 :(得分:0)
#!/usr/bin/env bash
mysql -u user -ppassword database -e "select distinct entityName,entitySource from AccessControl" | while read name source; do
echo "entityName: $name, entitySource: $source"
done
答案 2 :(得分:0)
exec
修复了它。[wcuser@localhost]$ temp=`exec mysql -h10.10.8.36 --port=3306 -uwcuser -pwcuser@123 paycentral -e "select endVersion from script_execution_detail where releaseNo='Release1.0' and versionPrefix='PS'"|tail -1`
[wcuser@localhost]$ echo $temp