Reputation: 490
I have a script which will execute Insert Query n times - for that i have used FOR loop , but the problem is the command which connects to remote mysql also executes n times. Here is the script for the better idea for my problem.
#!/bin/bash -X
#fields: id|alias|booking_time|contact_no|deleted|grace|number_in_queue|pax|seated_time|status|walk_in_time|queue_id|user_id
echo "Bash version ${BASH_VERSION}..."
for i in {1..5..1}
do
_alias="Name$i"
_contact_no=$(cat /dev/urandom | tr -dc '1-9' | fold -w 10 | head -n 1)
_deleted="FALSE"
_number_in_queue=$i
_pax=$(( $RANDOM % 10 + 20 ))
_status="waiting"
_queue_id=424
_user_id=550
mysql -u root -p restbucks << EOF #Want this to execute only One time
INSERT INTO queue_item VALUES ('','$_alias',now(),'$_contact_no','$_deleted',NULL,'$_number_in_queue','$_pax',now(),'$_status',now(),'$_queue_id','$_user_id');
EOF
done
Everytime i try to run the script , it will ask me for the password. What i want is that only once the connection made.
Upvotes: 1
Views: 471
Reputation: 1980
You can move the mysql connect before the for loop.
mysql -u root -p restbucks << EOF #this execute only One time
for i in {1..5..1}
do
.....
done
EOF
Also it is recommended that you can write your queries into a file and then finally execute the file using single connection.
You can refer bulk-mysql-query
Upvotes: 1
Reputation:
That is because it is placed inside the for loop. Every time the control passes to the loop it gets executed. You don't need to connect to your server once you are connected. Try placing the statement before the FOR statement.
Upvotes: 0