如何获得像bash脚本mysql命令行适当的格式输出 [英] How to get proper tabular output like the mysql command line from a bash script

查看:177
本文介绍了如何获得像bash脚本mysql命令行适当的格式输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从一个bash脚本使用下面的查询查询数据库: -

I am querying a database from a bash script using following query:-

Output = echo "$QUERY_STR" | mysql -h $DB_HOST -u $DB_USER -p$DB_PASS $DB_NAME   

它给了我所需要的输出,我保存在变量

it gives me the required output which I save in a Variable

然而,当我回声 $输出我没有得到正确的格式化输出像MySQL查询的命令行。

However when I echo $output I do not get proper formatted output like in command line of mysql query.

阅读帖子之一来使用 -t 查询但是,对于大型数据集不给予适当的输出。

Read one of the post to use -t in the query however for large data set it does not give proper output.

要解决它,我保存在一个.csv文件输出。

To work around it, I am saving the output in a .csv file.

推荐答案

要维持所有这确实是保存在变量的值中的空白,它是双引号的变量是至关重要的:

To maintain all the whitespace that is indeed kept in the variable's value, it is crucial to double-quote the variable:

echo "$output"

此外,你不能有等号周围的空白的变量赋值:

Also, you cannot have whitespace around the equal sign in a variable assignment:

output=$(mysql ... <<< "$QUERY_STR")

这篇关于如何获得像bash脚本mysql命令行适当的格式输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆