【问题标题】:MYSQL differs in Output from scriptMYSQL 与脚本的输出不同
【发布时间】:2012-11-27 06:38:03
【问题描述】:

我在执行 bash 脚本中的命令时遇到了 MySQL 输出格式问题。

如果我在命令行上执行一个命令,我就能得到预期格式的输出。

$ mysql -u dbclient -pxxxx GEKONYLOGDB -e "select now(),max(time_stamp) from metrics"
+---------------------+---------------------+
| now()               | max(time_stamp)     |
+---------------------+---------------------+
| 2012-12-09 14:25:38 | 2012-12-09 14:25:20 |
+---------------------+---------------------+

但是如果我在脚本中保留相同的命令并执行,我没有得到格式化的输出。

$ cat test
#!/bin/bash
mysql -u dbclient -pxxxx GEKONYLOGDB -e "select now(),max(time_stamp) from metrics"

$ ./test
now()   max(time_stamp)
2012-12-09 14:27:52     2012-12-09 14:27:47 

所以我只需要脚本的相同输出。

谢谢。

【问题讨论】:

    标签: mysql linux bash


    【解决方案1】:

    传递-t--table 选项以强制表输出。

    mysql --table -u dbclient -pxxxx GEKONYLOGDB -e "select now(),max(time_stamp) from metrics"
    

    来自mysql --help

      -t, --table         Output in table format.
    

    【讨论】:

    • 非常感谢,它正在工作并提供预期的输出。
    【解决方案2】:

    如果您想在调用 mysql 程序时默认启用 --table 选项,请查看此 SO 答案How to store MySQL results to a file in table format

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-21
      • 1970-01-01
      • 1970-01-01
      • 2012-07-17
      • 2016-06-17
      • 1970-01-01
      相关资源
      最近更新 更多