简体   繁体   English

BASH Mysql输出语句的一部分

[英]BASH Mysql outputs part of statement

I'm trying to run the below script to get a list of Orgs and the amount of users each Org has: 我正在尝试运行以下脚本来获取组织列表以及每个组织拥有的用户数量:

#!/bin/bash

array=(293 182 177 12 85 51 325 225 40 169 357 329 243 349 291 295 22 279 16 69 219 299 301 331 91 281 285 59 283 341 45 289 95 61 77 13 14 201 43 343 223 28 171 26 233 47 303 367 369 339 257 305 353 245 213 87 345 2 71 199 24 179 259 37 35 237)

for i in "${array[@]}"; do   

query=$(export MYSQL_PWD=MYPASS; mysql -e "SELECT COUNT(*) FROM Org.Users WHERE HomeOrgID=$i;")

echo "Org_$i:$query"  

done

I expect the result to be a simple "Org:Number" list: 我希望结果是一个简单的“ Org:Number”列表:

Org_1:150
Org_25:250
Org_17:64
Org_64:12

But the output is not displayed correctly. 但是输出显示不正确。 It does show part of the MySQL statement: 它确实显示了MySQL语句的一部分:

Org_293:COUNT(*)
1
Org_182:COUNT(*)
0
Org_177:COUNT(*)
8
Org_12:COUNT(*)
0
Org_85:COUNT(*)
1

How can I get the output to NOT display that "COUNT(*)" and display just a simple list? 如何使输出不显示“ COUNT(*)”而仅显示一个简单列表?

Thanks in advance! 提前致谢!

One quick hack : use bash arrays to store your output. 一个快速的技巧:使用bash数组存储您的输出。

#!/bin/bash

array=(293 182 177 12 85 51 325 225 40 169 357 329 243 349 291 295 22 279 16 69 219 299 301 331 91 281 285 59 283 341 45 289 95 61 77 13 14 201 43 343 223 28 171 26 233 47 303 367 369 339 257 305 353 245 213 87 345 2 71 199 24 179 259 37 35 237)

for i in "${array[@]}"; do  
    # we store the result of the request as an array
    query=($(export MYSQL_PWD=MYPASS; mysql -e "SELECT COUNT(*) FROM Org.Users WHERE HomeOrgID=$i;"))

    # we echo the 2nd member of the array. ${query[0]} should contain 'count(*)'
    echo "Org_$i:${query[1]}"

done

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

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