The UNIX and Linux Forums  

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
Problem in For Loop The Observer Shell Programming and Scripting 2 05-27-2008 11:43 PM
for loop problem namishtiwari Shell Programming and Scripting 5 01-25-2008 09:58 AM
loop problem maskot Shell Programming and Scripting 1 05-25-2007 01:10 AM
Problem with for loop/sed ? chiru_h Shell Programming and Scripting 2 08-27-2006 08:55 AM
problem with while loop mridula High Level Programming 1 12-11-2005 08:44 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 06-21-2006
Registered User
 

Join Date: Jun 2006
Posts: 1
Post How to pass values from select statement to a shell script variable

Connected to oracle database
sqlplus << EOF
$CONNECTSTR
set heading off
set trimspool on
set feedback off

select ID,DATE from sysadm.TEST where VALUE = 'A' order by ID;

value_id = ID
value_date = DATE
EOF

1. Is it possible to reference the values, ID,DATE in unix shell script.
2. Is it possible to loop through the select statement incase the select statement returns 10 rows.
3. if the only option is writting the values from select statement to a flat file, then the above select statement writes the 10 rows in the flat file.
Reply With Quote
Forum Sponsor
  #2  
Old 06-21-2006
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,326
Try piping the whole select statement to a 'while read ...'. I think that should work.
Reply With Quote
  #3  
Old 05-01-2007
Registered User
 

Join Date: May 2007
Posts: 10
Problem with while loop and SQL

hi,

i am trying to do a unix script and this is my first time getting in touch with unix.

i am trying to query and execute the following:
====================================================

touch $dir/emailList.txt
set final = $dir/emailList.txt

#construct SQL statement

$DCITS_SQL << SQLSTAT
set line=(select a.FIN,a.STAFF_NAME_X,b.FIN,b.DEPT_C from ci_5day_staff a, ci_cits_consol_dtls b where a.FIN=b.FIN);
SQLSTAT

while read ($line)

#pipe the output to while read
do
if ["$line"]#check if line is not null
then
#if line not null, parse the line into words/variables
set $line #set the line to positional variables, in this case is $1 and $2
fin ="$1" #staff fin number
name="$2" #name of staff
gway="$3"
echo '$fin,$name,$gway' >> $final
endif
done
=======================================================
i keep having the error "line=undefined variable". any one know whats wrong with the above? many thanks in advance!
Reply With Quote
  #4  
Old 05-02-2007
Registered User
 

Join Date: May 2007
Posts: 10
Quote:
Originally Posted by blowtorch
Try piping the whole select statement to a 'while read ...'. I think that should work.
hi,

i tried piping the whole thing to while read but there are still some errors:

#construct SQL statement

{$DCITS_SQL << EOF
set head off
select a.FIN,a.STAFF_NAME_X,b.FIN,b.DEPT_C from ci_5day_staff a, ci_cits_consol_dtls b where a.FIN=b.FIN;
exit
EOF
}|while read line

do
if ["$line"]#check if line is not null
then
#if line not null, parse the line into words/variables
set $line #set the line to positional variables, in this case is $1 and $2
fin ="$1" #staff fin number
name="$2" #name of staff
gway="$3"
echo '$fin,$name,$gway' >> $final
endif
done

please help? thanks!
Reply With Quote
  #5  
Old 05-02-2007
Playing with Ubuntu Now!
 

Join Date: Oct 2005
Location: Chennai
Posts: 364
"if" syntax

Check the 'if' syntax. There should be a space before and after the "line".
Code:
if [ "$line" ]
then
...
fi
Use 'fi' instead of 'endif'
Please post any errors you get for that would give a clue where the issue is.
Reply With Quote
  #6  
Old 05-02-2007
Registered User
 

Join Date: May 2007
Posts: 10
Quote:
Originally Posted by ranj@chn
Check the 'if' syntax. There should be a space before and after the "line".
Code:
if [ "$line" ]
then
...
fi
Use 'fi' instead of 'endif'
Please post any errors you get for that would give a clue where the issue is.
hi,

so glad to see ur reply

here's my error

Missing }
}: Command not found
while: Expression syntax


thanks!
Reply With Quote
  #7  
Old 05-02-2007
Playing with Ubuntu Now!
 

Join Date: Oct 2005
Location: Chennai
Posts: 364
code changes

The changes are marked in bold.
Code:
{
$DCITS_SQL << EOF
set head off
select a.FIN,a.STAFF_NAME_X,b.FIN,b.DEPT_C from ci_5day_staff a, ci_cits_consol_dtls b where a.FIN=b.FIN;
exit
EOF
}|while read line
do
if [ "$line" ] #check if line is not null
then 
#if line not null, parse the line into words/variables
set $line #set the line to positional variables, in this case is $1 and $2
fin ="$1" #staff fin number
name="$2" #name of staff
gway="$3" 
echo "$fin,$name,$gway" >> $final #changing single quotes to double
fi   # not endif
done
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:04 PM.


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

Content Relevant URLs by vBSEO 3.2.0