LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 05-25-2011, 09:14 AM   #1
swathikumar
LQ Newbie
 
Registered: May 2011
Posts: 9

Rep: Reputation: Disabled
Assigning query result into a variable/file


Hi
I am connecting to MySQL DataBase using shellscript and writing a select statement like
select attachmentid from attachments where pageid=10175

I want to store the output of the select statement into a shellvariable or a file

How can i do it using shellscript.

Please guide me.
 
Old 05-25-2011, 10:53 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,185

Rep: Reputation: 1782Reputation: 1782Reputation: 1782Reputation: 1782Reputation: 1782Reputation: 1782Reputation: 1782Reputation: 1782Reputation: 1782Reputation: 1782Reputation: 1782
Use $() and place your command in between to assign to a variable or > to redirect output to a file.
 
Old 05-25-2011, 07:28 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994
Useful shell links
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 05-26-2011, 01:38 AM   #4
swathikumar
LQ Newbie
 
Registered: May 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
Assigning select statement output into a variable/file

Thanks All,

I got the solution for this using like


mysql -h $host_name -u $username -p$password -D $db -e "select * from spaces where SPACENAME='AAAT Rollout'" > /Users/Desktop/test11

change the permission of the file chmod 757 filename
 
Old 05-26-2011, 02:33 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,185

Rep: Reputation: 1782Reputation: 1782Reputation: 1782Reputation: 1782Reputation: 1782Reputation: 1782Reputation: 1782Reputation: 1782Reputation: 1782Reputation: 1782Reputation: 1782
Well I am not sure why you would make it world executable but not group, however, glad you found a solution.
Please mark as SOLVED.
 
  


Reply

Tags
bash, command line, command substitution


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to copy query result to to csv file tucs_123 Programming 1 12-20-2008 12:45 PM
Trouble assigning value to variable with fatsheep Programming 1 11-07-2006 07:44 PM
assigning commands to a variable aunquarra Linux - General 5 01-04-2006 07:58 AM
mySQL redirecting query result to a .txt file! buttersoft Linux - Software 8 12-12-2003 03:05 AM


All times are GMT -5. The time now is 04:49 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration