简体   繁体   English

如何在脚本中转义 *

[英]how to escape * in a script

I am trying to write a little bash script with a function doing a query:我正在尝试用 function 编写一个小的 bash 脚本来执行查询:

query(){
cmd="mysql -N -B -u $user -p$password -h $host -e \"$q;\"" #2>&1";
eval $cmd
}

with

q="select * from mydatabase.mytable" q="从 mydatabase.mytable 中选择 *"

and calling query the *-character messes everything up and expands to a listing of the current directory.并调用 query * -字符将所有内容搞乱并扩展为当前目录的列表。 How can I avoid this?我怎样才能避免这种情况? The function should accept any valid sql-statement. function 应该接受任何有效的 sql 语句。 Thanks i advance for any tip.谢谢我提前提供任何提示。 H H



Disabling globbing with set -f seems to solve the problem:使用 set -f 禁用 globbing 似乎可以解决问题:

query(){ set -f cmd="mysql -N -B -u $user -p$password -h $host -e "$q;"" #2>&1"; echo $cmd eval $cmd set +f } query(){ set -f cmd="mysql -N -B -u $user -p$password -h $host -e "$q;"" #2>&1"; echo $cmd eval $cmd set +f }

Is this the best way?这是最好的方法吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM