473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Extracting Field data and putting each field into a shell variable?

I have forgotten a lot about SQL and would like to be (gently) reminded how
to do this:
In a MySQL query on the database (one table with 15 variable length fields,
I want to put each field into a Bash variable so that I can handle each
field as an entity.

The query I have is something like this:
mysql -e "use $database; select field1, field2, field3..., from Table1 where
fieldN like '%something%';"

can I / How do I do something like:
mysql -e "use $database; select field1,..., var1=field1, var2=field2,....;"

do
stuff with var1, var2...,
done

I ask because SQL is subtle and quick to anger and I'm in a bit of a hurry.

Thanks!
--
If you wrestle in the mud with a pig.
you both get dirty, and the pig likes it.

-- Dave Dawson
Jun 19 '06 #1
2 6758
David Dawson wrote:
In a MySQL query on the database (one table with 15 variable length fields,
I want to put each field into a Bash variable so that I can handle each
field as an entity.


I'd use mysql --batch to output the columns tab-separated, and then use
the shell builtin command "read" to assign several variables:

mysql --batch -e 'select col1, col2, col3 from...' | read col1 col2 col3
echo "$col1, $col2, $col3"

If your data contain whitespace, it might confuse the above method. You
might benefit from using the "--vertical" option of the mysql client
tool, to output the SQL columns one per line, instead of tab-separated.

mysql --vertical -e 'select col1, col2, col3 from...' | {
read col1
read col2
read col3
}

Note that the shell can group commands in the current shell by using
curly braces {}, or in a subshell by using parens (). Variables
assigned values in a subshell evaporate when the subshell ends.

See docs at
http://dev.mysql.com/doc/refman/5.0/...d-options.html

Regards,
Bill K.
Jun 19 '06 #2
Bill Karwin wrote:
David Dawson wrote: Note that the shell can group commands in the current shell by using
curly braces {}, or in a subshell by using parens (). Variables
assigned values in a subshell evaporate when the subshell ends.

See docs at
http://dev.mysql.com/doc/refman/5.0/...d-options.html

Regards,
Bill K.

Thanks a lot!
I had intended to re-write this application in Perl, towards which I wrote a
small script to handle this very query with the cpan DBI package and the
Mysql class, but this give me a neat alternative to explore
..--
If you wrestle in the mud with a pig.
you both get dirty, and the pig likes it.

-- Dave Dawson
Jun 20 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steve | last post by:
Hi, I have a very long string, someting like: DISPLAY=localhost:0.0,FORT_BUFFERED=true, F_ERROPT1=271\,271\,2\,1\,2\,2\,2\,2,G03BASIS=/opt/g03b05/g03/basis,...
1
by: v0lcan0 | last post by:
Any help on extracting the time part from the datetime field in SQL database. even though i had entered only the time part in the database when i extract the field it gives me only the date...
3
by: tripyre | last post by:
I have been trying to pull the data from a field on a form in Access. I am trying to pass that value to a shell command. Below is my code, but I cannot get it to work. The ping command works but...
13
by: MLH | last post by:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer) If LaborCost > 0 Then Me!LaborCost.Visible = True If MatlsCost > 0 Then Me!MatlsCost.Visible = True If OtherCost > 0 Then...
4
by: dor | last post by:
i have an input file named input.txt where all the data looks like this: (4,10) 20 (5,3) 13 (7,19) 6 .. .. .. the numbers are random. i need to use every number in each line
2
by: Brett | last post by:
My database has 2 tables: Table1 & Table2. If a field is not null on a record in table2, then the not null fields in table1 that correspond to the records in table1 needs to be updated to match the...
4
by: Debbiedo | last post by:
My software program outputs an XML Driving Directions file that I need to input into an Access table (although if need be I can import a dbf or xls) so that I can relate one of the fields...
1
by: swanc04 | last post by:
Greetings Master of PHP /bow I would like to preform the following task but I'm unable to figure out how, I never really got into Arrays and usually just use work arounds. I would be very greatful...
1
DebadattaMishra
by: DebadattaMishra | last post by:
Introduction In case of rich applications, you must have observed that a text field behaves like a dynamic combo box. When the user enters some characters in the text field, a popup will come up...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.