1

I have a variable in a script with the list of folder names with dash in their names

DBDIR="some directory"
dbdash=`ls -l $DBDIR | egrep '^d' | grep '.-.' | awk '{print $9}'`

And I want to drop every database with the same names with the folders in the variable "dbdash"

dbhype=($dbdash)
    for dbtry in ${!dbhype[*]}
        do            
            mysqladmin -u$dbUser -p$dbPass 'drop database if exists `'${dbhype[$dbtry]}'` CHARACTER SET utf8 COLLATE utf8_general_ci;'
    done

But this error came out after running the script

mysqladmin: Unknown command: 'drop database if exists folder_name CHARACTER SET utf8 COLLAT'

How can I delete these databases using script? Please help.

1
  • Don't parse ls, and don't try to store a list of files in a flat string. cd "$DBDIR"; dbhype=( *-*/ ) Commented Feb 24, 2016 at 13:48

1 Answer 1

1

Backquotes are for executing commands; you just want to quote the value (although the way you construct dbhype, quoting is too late to do you any good).

cd "DBDIR"
for dbtry in *-*/; do
    mysqladmin -u "$dbUser" -p "$dbPass" "drop database if exists '$dbtry'"
done
Sign up to request clarification or add additional context in comments.

1 Comment

thank you @chepner for this answer. i tried using this and edited a bit of it. I use this: mysql -u$dbUser -p$dbPass -e 'drop database if exists '$dbtry'' instead of: mysqladmin -u "$dbUser" -p "$dbPass" "drop database if exists '$dbtry'" and it worked!

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.