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

查看:19
本文介绍了如何执行存储为带引号和星号的字符串的 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 query ;-)

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

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

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