【问题标题】:Receive results of select in shell在 shell 中接收 select 的结果
【发布时间】:2014-10-27 12:41:00
【问题描述】:

好的,我有这样的脚本

#!/bin/bash
keyOrPass=$1
intercom=$2
flat=$3
number=$4
mysql -ulogin -ppass db_name -e "select cli.codeGuestEmail, cli.codePrivateEmail, cliKey.rf_id, cliKey.emailNotification from mbus_clients as cli join mbusClientKeys as cliKey on cliKey.id_client=cli.id WHERE cli.flat=${flat} and cli.domophone=${intercom};";
php -q sendNotifications.php

它工作正常,但我应该将一些结果字段从 select 传递到 php 参数。有什么想法吗?

输出:

+----------------+------------------+------------+-------------------+ 
| codeGuestEmail | codePrivateEmail | rf_id      | emailNotification |
+----------------+------------------+------------+-------------------+
|              1 |                0 | 2774490192 |                 0 |
|              1 |                0 | 2774490193 |                 0 |
|              1 |                0 | 2774490194 |                 0 |
|              1 |                0 | 2774490195 |                 1 |
+----------------+------------------+------------+-------------------+

【问题讨论】:

  • 什么?为什么不在 php 中执行此操作?
  • 是否可以通过inetd运行php文件?
  • 你为什么要这样做?如果你想传递结果,那么你可以编写一个命令行 PHP 脚本,或者你可以从 PHP 访问 MySQL。您可以在某些 Linox 操作系统上将 PHP 脚本作为服务运行。
  • 添加选择命令的示例输出。
  • "是否可以通过 inetd 运行 php 文件?" - 是的。

标签: php mysql bash shell


【解决方案1】:

mysql 能够生成不同格式的输出。使用-B--batch 选项,mysql 生成以TAB 作为列分隔符的输出。字段中的特殊字符被转义(例如,TAB 输出为“\t”),因此您可以使用cut 来提取字段。 很多时候,在这种情况下,使用 -N--skip-column-names 选项也很有帮助,以便从输出中删除列名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 2011-06-04
    • 1970-01-01
    相关资源
    最近更新 更多