Connecting Tech Pros Worldwide Help | Site Map

How to store Mysql Database Field value to Shell Script variable

Member
 
Join Date: Nov 2008
Posts: 35
#1: Jan 2 '09
Hi Bytes.. i have need to assign Mysql Database DataField value to shell script variable .

is it possible ? i have tried like this .. got error..
Expand|Select|Wrap|Line Numbers
  1. #!bin/sh
  2.  
  3. password=xxx
  4. mysql -u root -p$password -h 192.168.1.8 << y
  5. use dbssys;
  6. d ="`select mem from profile` ";
  7. echo $d
  8. quit
  9. y
  10.  
  11.  
Got Output like this..

test.sh: command substitution: line 1: syntax error near unexpected token `from'
test.sh: command substitution: line 1: `select mem from profile'
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 'd =" "' at line 1
micmast's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: Belgium
Posts: 136
#2: Jan 2 '09

re: How to store Mysql Database Field value to Shell Script variable


The problem is that the backticks will execute the code within and select mem from profile is no valid linux bash/shell command. Try escaping them with a \
Member
 
Join Date: Nov 2008
Posts: 35
#3: Jan 2 '09

re: How to store Mysql Database Field value to Shell Script variable


i cant get you clearly.. if you dont mind..can you please correct that particular line and send me ..
micmast's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: Belgium
Posts: 136
#4: Jan 2 '09

re: How to store Mysql Database Field value to Shell Script variable


micmast@plato:~$ d="\`select mem from profile\`";
micmast@plato:~$ echo $d
`select mem from profile`
Member
 
Join Date: Nov 2008
Posts: 35
#5: Jan 6 '09

re: How to store Mysql Database Field value to Shell Script variable


Hi i didnt solve the problem yet.. after applying changes that you have said.. any other solution for this..
gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,256
#6: Jan 7 '09

re: How to store Mysql Database Field value to Shell Script variable


Why canyou use isql or fsql to connect to the DB and get the values to the variable you expect?

raghu
Member
 
Join Date: Nov 2008
Posts: 35
#7: Jan 7 '09

re: How to store Mysql Database Field value to Shell Script variable


i am not using fsql or isql for my project. actually i am doing Linux system oriented project and storing the values in Mysql . i need to compare the values that stored in mysql and recent values get from the linux .. so thats why i am looking for a solution? can u suggest some hint ? method /way proceed further??
ashitpro's Avatar
Expert
 
Join Date: Aug 2007
Posts: 389
#8: Jan 7 '09

re: How to store Mysql Database Field value to Shell Script variable


Try using this line...

result=$(mysql -u $user_name $db_name -p$password -sN -e “select mem from profile”)
Reply