The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM
Home Forums Register Rules & FAQ Members List Arcade Search Today's Posts Mark Forums Read


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 !!


Other UNIX.COM Threads 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 11:00 AM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-19-2007 09:52 PM
Avoid "++ requires lvalue" Error in Loop Calculation sandeepb Shell Programming and Scripting 3 09-24-2007 04:02 AM
Unix "at" / "Cron" Command New Problem...Need help Mohanraj UNIX for Dummies Questions & Answers 3 01-26-2006 04:08 PM
How to combine "find" command in for each loop (tcsh) umen Shell Programming and Scripting 3 08-22-2005 01:07 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-03-2007
Registered User
 

Join Date: Sep 2007
Posts: 7
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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.....
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-03-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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
Reply With Quote
Google UNIX.COM
Reply



Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB 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 -7. The time now is 05:44 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger

Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102