Looping/Reading file contents not working | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


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

Looping/Reading file contents not working

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-30-2013
DBnixUser DBnixUser is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 31 January 2013, 3:38 PM EST
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Looping/Reading file contents not working

Hi,

I am doing something basic, but I am missing something. Im trying to read the contents of a file and taking those values and connecting to a database. However, it only connect to one (or reads in) value and then exists.

Here is what it looks like:

listname.txt

Code:
db1
db2
db3

Script:

Code:
dbcontent=/usr/home/listname.txt
echo "Enter userid" 
read USERID
echo "Enter passwd:"
stty -echo
read -s PASSWD
stty echo

while read dbname;
do
#Connect to SQLPlus
sqlplus -S  test/test123@$dbname << EOF
select ora_database_name from dual;
prompt my name is $USERID and my password is $PASSWD;
exit;
EOF
done < $dbcontent

It doesn't read all the lines (loop through) just exits after one run.
I am also trying to have it connect to each database (in the list).

Last edited by DBnixUser; 01-30-2013 at 08:51 PM..
Sponsored Links
    #2  
Old 01-30-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 17 December 2014, 4:49 PM EST
Location: Galactic Empire
Posts: 3,395
Thanks: 240
Thanked 1,211 Times in 1,137 Posts

Code:
sqlplus -S  test/test123@$dbname << EOF

Sponsored Links
    #3  
Old 01-30-2013
DBnixUser DBnixUser is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 31 January 2013, 3:38 PM EST
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Sorry I forgot to add that. I have that already.
    #4  
Old 01-30-2013
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 18 December 2014, 5:01 AM EST
Location: In the leftmost byte of /dev/kmem
Posts: 4,370
Thanks: 47
Thanked 852 Times in 673 Posts
At first sight your code looks correct (save for some minor things which shouldn't matter here), so try to isolate the problem. What does


Code:
typeset dbcontent="/usr/home/listname.txt"
typeset dbname=""
echo "Enter userid" 
read USERID
echo "Enter passwd:"
stty -echo
read -s PASSWD
stty echo

while read dbname; do
     echo "DBNAME is: $dbname"
     #Connect to SQLPlus
     #sqlplus -S  test/test123@$dbname << EOF
     #select ora_database_name from dual;
     #prompt my name is $USERID and my password is $PASSWD;
     #exit;
#EOF
done < "$dbcontent"

do? Does it still go only once through the loop, then probably something with the input file is wrong (unprintable characters or the like). Is the problem gone, then something with the sqlplus-command is responsible. Either way you know more then than you know now.

I hope this helps.

bakunin
Sponsored Links
    #5  
Old 01-31-2013
DBnixUser DBnixUser is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 31 January 2013, 3:38 PM EST
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
No, this works just fine. I tried that also to make sure it is actually looping.

Thanks.
Sponsored Links
    #6  
Old 01-31-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 18 December 2014, 8:34 AM EST
Location: Aachen, Germany
Posts: 4,874
Thanks: 80
Thanked 1,220 Times in 1,146 Posts
Do you get an error (exit status != 0) from the DB operations? In bash (you unfortunately don't mention what system you use), setting the -e option (not shown in your code snippet) will immediately exit on an error condition.
Sponsored Links
    #7  
Old 01-31-2013
DBnixUser DBnixUser is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 31 January 2013, 3:38 PM EST
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by RudiC View Post
Do you get an error (exit status != 0) from the DB operations? In bash (you unfortunately don't mention what system you use), setting the -e option (not shown in your code snippet) will immediately exit on an error condition.
No it works fine. No errors are reported. It executes the script successfully, but only goes through one iteration....its not looping.

Sorry I am using korn shell (/bin/ksh).
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Reading file contents until a keyword infintenumbers Shell Programming and Scripting 2 07-31-2012 05:23 PM
While or For with looping contructs? to create files from contents of a text file wolf@=NK Shell Programming and Scripting 3 05-23-2012 07:15 AM
reading file,looping and limitation Jang Shell Programming and Scripting 5 10-28-2011 03:48 PM
Reading and printing one by one contents of a file Aditya.Gurgaon Shell Programming and Scripting 2 01-27-2009 06:08 AM
Reading specific contents from a file and appending it to another file dnicky Shell Programming and Scripting 5 10-04-2005 06:45 AM



All times are GMT -4. The time now is 10:26 AM.