LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell script function under mysql loop (https://www.linuxquestions.org/questions/programming-9/shell-script-function-under-mysql-loop-4175549213/)

san2roy 07-28-2015 09:58 PM

Shell script function under mysql loop
 
I am trying to get working a function under mysql loop but could not work. please help me

Code:

#!/bin/bash
# declare an array called array and define 3 vales
# ind-func.sh: Passing an indirect reference to a function.
USERNAME=xbt
PASSWORD=xbt
DATABASE=xbt
myvariable=$(mysql -u $USERNAME -p$PASSWORD -s -N <<QUERY_INPUT
    use $DATABASE;
    SELECT HEX(info_hash),fid FROM xbt_files WHERE status="0";
QUERY_INPUT
)
#echo $myvariable
echo_var ()
{
deluge-console add  magnet:?xt=urn:btih:"$1" #this is another function need to loop
mysql -u $USERNAME -p$PASSWORD -D $DATABASE -e "UPDATE xbt_files SET status = '1' WHERE fid = $2;"
}
cnt=${#myvariable[@]}
for (( i=0 ; i<${cnt} ; i++ ))
do
echo_var ${myvariable[0]} ${myvariable[1]}
done

what's the wrong i am doing?

Thanks you

grail 07-30-2015 12:37 PM

Might need a little more information, such as, exactly what is not working?

michaelk 07-30-2015 01:45 PM

Welcome to LinuxQuestions.

I am not a mysql user and therefore will make a few assumptions.

myvariable is a string not an array. I expect ${myvariable[0]} to be the entire string and ${myvariable[1]} to be blank. What does mysql use as a field separator in silent mode (-s) ie. a comma (,)?

I assumed there could be more then one row from your query so used tr to replace new lines with a "," which would allow you to split them into and array and iterate them as desired.

Code:

test=$( echo $myvariable | tr "\n " "," )
IFS=',' read -a stuff <<< "${test}"
cnt=${#stuff[*]}
for (( i=0 ; i<${cnt} ; i+=2 ))
do
    echo_var ${stuff[$i]},${stuff[$i+1]}
done


MfromH 08-06-2015 04:28 AM

in the second mysql call you can leave out -D, every string beside options is automatically interpreted as a database name.

Or try lower case -d .


All times are GMT -5. The time now is 05:43 PM.