【问题标题】:Using bash to get multiple MYSQL records使用 bash 获取多条 MYSQL 记录
【发布时间】:2013-09-29 19:09:29
【问题描述】:

我正在尝试从 mysql 数据库中获取多条记录,并将它们输出到我拥有的文本文件的单行中

#!/bin/bash
result_storage="/tmp/book"
id=$(mysql --host=host --user=user --password=password --database=db -s -N -e "selectcount(*) from people")
echo -e "$id New" > $result_storage

id2=$(mysql --host=host --user=user --password=password --database=db -s -N -e "select count(*) from numbers")
sed '1 s|$|"$id2" Numbers|' -i $result_storage

但是 sed 部分打印 $id2 而不是来自 id2 的计数

基本上最后我想 /tmp/book 阅读

12 个人 10 个号码

【问题讨论】:

    标签: mysql bash shell sed


    【解决方案1】:

    试试下面

    mysql --host=host --user=user --password=password --database=db -s -N -e "selectcount(*) from people; select count(*) from numbers;"
    

    我没有在上面执行,但很可能它会工作......

    【讨论】:

    • 不适合我,也不允许我像 echo -e "$id New" 那样将我的文本放在文件中
    • 请您编辑您的问题并显示您需要的示例输出
    【解决方案2】:

    变化

    sed '1 s|$|"$id2" Numbers|' -i $result_storage
    

    sed "1 s|$| $id2 Numbers|" -i $result_storage
    

    应该可以,因为 bash 需要双引号来执行变量替换。

    【讨论】:

    • 非常接近,但它输出“10”个成员,即带引号 12 人“10”个数字
    • 如果您不想要引号,则只需删除它们,就像我在回答中所做的那样。
    猜你喜欢
    • 1970-01-01
    • 2017-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    相关资源
    最近更新 更多