简体   繁体   中英

Unable to use while command in Windows Command Prompt

What I'm trying to accomplish is dumping each of the databases I have into individual .sql files. Whenever I run my code, I get an error in relation to the 'while' and 'do' commands depending on which one I put first. I've since examples where the 'while' is first and where the 'do' is first. I have tried both and both result in the 'not recognized as an internal or external command.

I'm using the following in my cmd line

"C:\Program Files\MySQL\MySQL Server 5.7\bin\"mysql -u[user] -p[password] -e "show databases" | while read dbname do "C:\Program Files\MySQL\MySQL Server 5.7\bin\"mysqldump -u[user] -p[password] --complete-insert "$dbname" > "D:\Backup\$dbname"_6am_mon.sql; done

However, I keep getting the following error: 'while' is not recognized as an internal or external command, operable program or batch file.

Why am I getting an error for using the while and do commands? How do I resolve this error?

Thanks

The pipe | and the output redirection > are interpreted by cmd.exe first and not viewed as part of the script. Try to escape them with a caret ^| and ^>

Not knowing much about Mysql script processing the quoting of the redirection file name may be the reason $dbname is not resolved.

A workaround could be to put the script in a file and invoke it from the batch

@Echo off
Pushd "C:\Program Files\MySQL\MySQL Server 5.7\bin\"
mysql -u[user] -p[password] -e "Source X:\Path\Script.sql"
Popd

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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