LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-28-2015, 09:58 PM   #1
san2roy
LQ Newbie
 
Registered: Jul 2015
Posts: 1

Rep: Reputation: Disabled
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
 
Old 07-30-2015, 12:37 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Might need a little more information, such as, exactly what is not working?
 
1 members found this post helpful.
Old 07-30-2015, 01:45 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
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
 
Old 08-06-2015, 04:28 AM   #4
MfromH
LQ Newbie
 
Registered: Jul 2014
Distribution: openSuse 12.2, SLES11.3, RHEL 5,
Posts: 15

Rep: Reputation: Disabled
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 .
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] running shell script loop eminempark Programming 4 10-27-2013 11:57 PM
shell script for each loop s_linux Programming 4 07-02-2013 04:50 PM
Call Shell Function from mysql santhoshvkumar Programming 2 04-18-2011 02:15 PM
shell script , while loop ykc Programming 5 03-30-2009 07:50 AM
Loop in Shell Script delamatrix Programming 4 07-24-2008 05:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration