Reputation: 63
#!/bin/bash
results=($(mysql --user xxx -pxxxxx asterisk -Bse "SELECT id,did From global_did WHERE status='no' LIMIT 1 ;"))
cnt=${#results[@]}
for (( i=0 ; i<cnt ; i++ ))
do
cat << EOFTEST1 > /etc/asterisk/script/$((1 + RANDOM % 1000))-255621067201
Channel: Local/255621067201@dialer
MaxRetries: 0
WaitTime: 30
Context: informer
Extension: 255621067201
Callerid: ${results[1]}
Account: Tanzania
Priority: 1
EOFTEST1
UPDATED=${results[0]}
mysql --user=xxxx --password=xxxx asterisk -Bse "UPDATE global_did SET status='yes' WHERE id='${UPDATED}';"
I set it to have a random number $((1 + RANDOM % 1000)) when executing it, it generates two files instead of one,what am I doing wrong?
Upvotes: 0
Views: 36
Reputation: 781059
The for
loop is executing once for each column being returned by the query. Since the query returns two columns, $cnt
is 2
, and you execute the code twice, and get two files. Just get rid of the loop.
If you want to allow the query to return more than one row, and perform the loop for each row, you should increment $i
by 2, not 1, and use $i
in the array indexes rather than hard-coding 1
and 2
.
for (( i=0 ; i<cnt ; i+=2 ))
do
cat << EOFTEST1 > /etc/asterisk/script/$((1 + RANDOM % 1000))-255621067201
Channel: Local/255621067201@dialer
MaxRetries: 0
WaitTime: 30
Context: informer
Extension: 255621067201
Callerid: ${results[$i+1]}
Account: Tanzania
Priority: 1
EOFTEST1
UPDATED=${results[$i]}
mysql --user=xxxx --password=xxxx asterisk -Bse "UPDATE global_did SET status='yes' WHERE id='${UPDATED}';"
done
Upvotes: 1