如何执行存储为带引号和星号的字符串的 bash 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2005192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to execute a bash command stored as a string with quotes and asterisk
提问by Barth
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 的帮助页面:
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.
我想我对引号做了一些明显错误的事情,但无法找出问题所在。
回答by slebetman
Have you tried:
你有没有尝试过:
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'"
Bonus: It also reads nice: eval mysql query ;-)
奖励:它也读起来不错:eval mysql query ;-)
回答by Charles Duffy
Use an array, not a string, as given as guidance in BashFAQ #50.
使用数组,而不是字符串,如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 to eval
a password containing $(rm -rf .)
!
使用字符串是非常糟糕的安全实践:考虑用户提供的情况password
(或查询中的 where 子句,或任何其他组件);您不想eval
密码包含$(rm -rf .)
!
Just Running A Local Command
只运行一个本地命令
cmd=( mysql AMORE -u username -ppassword -h localhost -e "SELECT host FROM amoreconfig" )
"${cmd[@]}"
Printing Your Command Unambiguously
明确打印您的命令
cmd=( mysql AMORE -u username -ppassword -h localhost -e "SELECT host FROM amoreconfig" )
printf 'Proposing to run: '
printf '%q ' "${cmd[@]}"
printf '\n'
Running Your Command Over SSH (Method 1: Using Stdin)
通过 SSH 运行您的命令(方法 1:使用标准输入)
cmd=( mysql AMORE -u username -ppassword -h localhost -e "SELECT host FROM amoreconfig" )
printf -v cmd_str '%q ' "${cmd[@]}"
ssh other_host 'bash -s' <<<"$cmd_str"
Running Your Command Over SSH (Method 2: Command Line)
通过 SSH 运行您的命令(方法 2:命令行)
cmd=( mysql AMORE -u username -ppassword -h localhost -e "SELECT host FROM amoreconfig" )
printf -v cmd_str '%q ' "${cmd[@]}"
ssh other_host "bash -c $cmd_str"
回答by ghostdog74
try this
尝试这个
$ cmd='mysql AMORE -u root --password="password" -h localhost -e "select host from amoreconfig"'
$ eval $cmd
回答by David Beckwith
You don't need the "eval" even. Just put a dollar sign in front of the string:
你甚至不需要“评估”。只需在字符串前面放一个美元符号:
cmd="ls"
$cmd
回答by Paul Havens
To eliminate the need for the cmd variable, you can do this:
要消除对 cmd 变量的需要,您可以执行以下操作:
eval 'mysql AMORE -u root --password="password" -h localhost -e "select host from amoreconfig"'