Reputation: 1383
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
Upvotes: 1
Views: 213
Reputation: 246764
(major edit)
The issue is with your use of quotes. In your code, since you do not quote $command
it is subject to word splitting by the shell. The $login command receives these arguments: "-e", "'drop", "table", "table_name'"
-- note the stray single quotes in the second and last elements.
Do this:
command="drop table $table"
drop=$($login -e "$command")
Upvotes: 1