In general, it's a bad idea to hard-code usernames/passwords in your scripts. Here are some ways to automatically discover the username/password to use.
Whenever possible, we focus on pulling the information from standard locations, locations where the username/pass is stored for other tools.
The ~/.my.cnf file is just an INI file, so any INI parser will do it. But there are specialized bits of parsing code too: