1

I'm trying to create a script to automatically delete all of the tables from a database using shell.

The commented out variable $drop works fine, however when I try to substitute in the table

for table in $tables
do
    command="'drop table ${table}'"

    # drop=$(${login} -e 'drop table test') -- this works fine
    drop=$(${login} -e $command)
    echo $drop
    # echo -e "Removed table ${table}"
done
4

1 回答 1

1

(主要编辑)

问题在于您使用引号。在您的代码中,由于您不引用$command它,因此它会被 shell 分词。$login 命令接收这些参数:"-e", "'drop", "table", "table_name'"-- 注意第二个和最后一个元素中的杂散单引号。

做这个:

command="drop table $table"
drop=$($login -e "$command")
于 2012-08-11T03:13:10.070 回答