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
2
  • turn on shell debugging to see what values are being used for $command, etc. add set -vx before your loop. to turn off, you can use set +vx. Good luck. Commented Aug 11, 2012 at 3:02
  • Hello, I used this and got these results:+ for table in '$tables' + set -vx + command=''\''drop table property_thumbnail'\''' + for table in '$tables' @shellter Commented Aug 11, 2012 at 3:10

1 Answer 1

1

(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")
Sign up to request clarification or add additional context in comments.

3 Comments

Hmm, I tried this and am still having problems. I don't think drop should be an array, rather, it takes $table from the $tables array. When I use the syntax shown above, the command does not show at all?
Current Code: ` command="drop table ${table}" drop=("$login" -e "${command}") echo "${drop[@]}"` which prints out the correct commands, but doesn't actually run the command. When I try $("${drop[@]}") it shows error "no such directory"@glenn jackman
@JonMorehouse, I misunderstood your question -- it is not clear what $login is. I have rewritten my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.