如何从MySQL查询结果在bash取现场 [英] How to fetch field from MySQL query result in bash

查看:139
本文介绍了如何从MySQL查询结果在bash取现场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得一个bash脚本MySQL查询结果仅取值。例如,运行以下命令:

 的mysql -uroot -ppwd -eSELECT ID FROM nagios.host其中name ='$主机'

返回:

  + ---- +
| ID |
+ ---- +
| 0 |
+ ---- +

我如何获取我的bash脚本返回的值?


解决方案

使用 -s -N

 > ID =`mysql的-uroot -ppwd -s -e -NSELECT ID FROM nagios.host其中name ='$主机'`
>回声的$ id
0

这本手册:


  

- 沉默,-S

 静音模式。产生更少的输出。此选项可以给多个
   次以产生越来越少的输出。   此选项的结果nontabular输出格式和转义
   特殊字符。转义可以使用原始模式被禁用;看到
   为--raw选项的描述。


  
  

- 跳列名,-N

 不要写在成绩列名。


修改

看起来 -ss 工程,以及,更容易记住。

I would like to get only the value of a MySQL query result in a bash script. For example the running the following command:

mysql -uroot -ppwd -e "SELECT id FROM nagios.host WHERE name='$host'"

returns:

+----+
| id |
+----+
| 0  |
+----+

How can I fetch the value returned in my bash script?

解决方案

Use -s and -N:

> id=`mysql -uroot -ppwd -s -N -e "SELECT id FROM nagios.host WHERE name='$host'"`
> echo $id
0

From the manual:

--silent, -s

   Silent mode. Produce less output. This option can be given multiple
   times to produce less and less output.

   This option results in nontabular output format and escaping of
   special characters. Escaping may be disabled by using raw mode; see
   the description for the --raw option.

--skip-column-names, -N

   Do not write column names in results.

EDIT

Looks like -ss works as well and much easier to remember.

这篇关于如何从MySQL查询结果在bash取现场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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