The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Development Releases: Linux Mint 4.0 Beta "Fluxbox", 4.0 Alpha "Debian" iBot UNIX and Linux RSS News 0 01-04-2008 03:00 PM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 01:52 AM
Avoid "++ requires lvalue" Error in Loop Calculation sandeepb Shell Programming and Scripting 3 09-24-2007 07:02 AM
Unix "at" / "Cron" Command New Problem...Need help Mohanraj UNIX for Dummies Questions & Answers 3 01-26-2006 08:08 PM
How to combine "find" command in for each loop (tcsh) umen Shell Programming and Scripting 3 08-22-2005 04:07 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-03-2007
Bob_Loblaw Bob_Loblaw is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 7
Unhappy "for" Loop Problem

Hello Everyone,

I'm having a wee bit of a problem. I would like to pass a DB2 query to a variable and then using that variable to perform a 'For' loop.

For example....
Code:
tempfile1=`db2 -x "select cast(OBJECT_ID as integer) from $temp_table where SEQUENCE_NO = $sequence_no"` 

for id in ${tempfile1}
do  

statename=`db2 -x "select STATE_NAME from $temp_table where object_id = $id"`
domain_name=`db2 -x "select domain_name from $temp_table where object_id = $id"`

echo "ID = [$id]"
echo "State = [$statename]"
echo "Domain = [$domain_name]"

done
The problem is when I run this if there are multiple object_id's with the same value but not the same state_name then when it's passed through an echo it looks like this....

Code:
ID = [5132]
State = [State1 State2]
Domain = [Domain1 Domain2]
ID = [5146]
State = [State1]
Domain = [Domain1]
ID = [5156]
State = [State1 State2]
Domain = [Domain1 Domain2]
ID = [5132]
State = [State1 State2]
Domain = [Domain1 Domain2]
ID = [5156]
State = [State1 State2]
Domain = [Domain1 Domain2]
However I would like it to look like this...
Code:
ID = [5132]
State = [State1]
Domain = [Domain1]
ID = [5146]
State = [State1]
Domain = [Domain1]
ID = [5156]
State = [State1]
Domain = [Domain1]
ID = [5132]
State = [State2]
Domain = [Domain2]
ID = [5156]
State = [State2]
Domain = [Domain2]
Would someone be able to see what I'm doing wrong.

Thanks in advance.....
  #2 (permalink)  
Old 10-03-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
Case 5132 gets returned twice by your first select because it's in two rows.

Then in the state select, two states get returned because there are two states associated with 5132.

I suggest doing something like....

Code:
db2 -x ..... | while read A B C D
do
....
done
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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



All times are GMT -4. The time now is 06:17 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0