如何执行存储为带引号和星号的字符串的 bash 命令
我尝试执行以下命令:
mysql AMORE -u username -ppassword -h localhost -e "SELECT host FROM amoreconfig"
我将它存储在一个字符串中:
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 的帮助页面:
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 try to execute the following command :
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\""
Test it :
echo $cmd
mysql AMORE -u username -ppassword -h localhost -e"SELECT host FROM amoreconfig"
Try to execute by doing :
$cmd
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您是否尝试过:
对于如何转义
*
的后续问题,因为它在裸露或双引号字符串中具有特殊含义:使用单引号。奖励:它读起来也不错:eval mysql query ;-)
Have you tried:
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.Bonus: It also reads nice: eval mysql query ;-)
使用数组,而不是字符串,如 BashFAQ #50 中给出的指导。
使用字符串是极其糟糕的安全实践:考虑一下
密码
(或查询中的where子句或任何其他组件)是用户提供的情况;您不想评估
包含$(rm -rf .)
的密码!仅运行本地命令
打印您的命令 明确地
通过 SSH 运行您的命令(方法 1:使用 Stdin)
通过 SSH 运行您的命令(方法 2:命令行)
Use an array, not a string, as given as guidance in BashFAQ #50.
Using a string is extremely bad security practice: Consider the case where
password
(or a where clause in the query, or any other component) is user-provided; you don't want toeval
a password containing$(rm -rf .)
!Just Running A Local Command
Printing Your Command Unambiguously
Running Your Command Over SSH (Method 1: Using Stdin)
Running Your Command Over SSH (Method 2: Command Line)
试试这个
try this
你甚至不需要“评估”。只需在字符串前面放置一个美元符号即可:
You don't need the "eval" even. Just put a dollar sign in front of the string:
要消除对 cmd 变量的需要,您可以这样做:
To eliminate the need for the cmd variable, you can do this: