如何执行存储与报价和星号的字符串bash命令 [英] How to execute a bash command stored as a string with quotes and asterisk

查看:138
本文介绍了如何执行存储与报价和星号的字符串bash命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试执行以下命令:

mysql AMORE -u username -ppassword -h localhost -e "SELECT  host  FROM amoreconfig"

我将其存储在一个字符串:

I store it in a string :

cmd="mysql AMORE -u username -ppassword -h localhost -e\"SELECT  host  FROM amoreconfig\""

测试吧:

echo $cmd
mysql AMORE -u username -ppassword -h localhost -e"SELECT host FROM amoreconfig"

试着做执行:

$cmd

和我获得MySQL的帮助页面:

And I get the help page of mysql :

mysql  Ver 14.14 Distrib 5.1.31, for pc-linux-gnu (i686) using readline 5.1
Copyright 2000-2008 MySQL AB, 2008 Sun Microsystems, Inc.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license
Usage: mysql [OPTIONS] [database]
(...)

我想我做一些简单的错误的报价,但不能查出是什么问题。

I guess I am doing something plain wrong with the quotes but can't find out what is the problem.

推荐答案

你试过:

eval $cmd

有关如何躲避后续问题 * ,因为它具有特殊的意义,当它的裸体或双引号的字符串:使用单引号

For the follow-on question of how to escape * since it has special meaning when it's naked or in double quoted strings: use single quotes.

MYSQL='mysql AMORE -u username -ppassword -h localhost -e'
QUERY="SELECT "'*'" FROM amoreconfig" ;# <-- "double"'single'"double"
eval $MYSQL "'$QUERY'"

奖励:它也读漂亮:EVAL MySQL查询; - )

Bonus: It also reads nice: eval mysql query ;-)

这篇关于如何执行存储与报价和星号的字符串bash命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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