Run Shell Commands after creating MYSQL Connection

Hi,
I am trying to make a Shell Script using which I will update around 10K records in MySQL db each night. For that, I am currently doing the following command in FOR LOOP:

mysql -D $MASTER_DB_NAME -h $MASTER_DB_HOST -u $MASTER_DB_USER -p$MASTER_DB_PASSWD  -e "$SQL_Query"

This command is unnecessarily making connection each time.

But, I want to know that how can I execute shell commands after creating the mysql connection so that I can create the DB Connection before the FOR LOOP and do the required tasks and then close the connection

For example:

mysql -u [user] -p[pass] << EOF
SHELL COMMAND
[mysql commands]
SHELL COMMAND
EOF

Please suggest some solution

I don't see why you would want to run shell commands and then mysql commands; however, you execute shell commands by using a quoted-bang....this works in a HERE document too.

mysql ...<<EOD
.
.
.
\! <shell-command-here>

EOD

I tried the method you shared by I couldn't succeed. I will brief out the layout of my code.

mysql -D $MASTER_DB_NAME -h $MASTER_DB_HOST -u $MASTER_DB_USER -p$MASTER_DB_PASSWD << EOD
\!for (( j=1;j<2000; j++ ))
\!do
..
..
..
<some usual Linux Script codes, which includes "if" conditions, wget, sed commands.>
<I am executing the sql command in for loop to generate values for it during run time>
..
..
update table_name set value1=$value1 , value_2=$value2 where id=$id'
\!done

EOD 

The error i got is

sh: 1: Syntax error: Bad for loop variable
ERROR 1064 (42000) at line 2: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'j<2000' at line 1