我想在bash脚本中只获取MySQL查询结果的值.例如,运行以下命令:
mysql -uroot -ppwd -e "SELECT id FROM nagios.host WHERE name='$host'"
Run Code Online (Sandbox Code Playgroud)
收益:
+----+
| id |
+----+
| 0 |
+----+
Run Code Online (Sandbox Code Playgroud)
如何获取bash脚本中返回的值?
Dor*_*mer 104
使用-s和-N:
> id=`mysql -uroot -ppwd -s -N -e "SELECT id FROM nagios.host WHERE name='$host'"`
> echo $id
0
Run Code Online (Sandbox Code Playgroud)
从手册:
--silent,-s
Run Code Online (Sandbox Code Playgroud)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
Run Code Online (Sandbox Code Playgroud)Do not write column names in results.
编辑
看起来像-ss工作也好,更容易记住.
更紧凑:
id=$(mysql -uroot -ppwd -se "SELECT id FROM nagios.host WHERE name=$host");
echo $id;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
87960 次 |
| 最近记录: |