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
Read csv into Hash array? kinmak Shell Programming and Scripting 1 05-07-2008 07:35 AM
create array holding characters from sring then echo array. rorey_breaker Shell Programming and Scripting 5 09-28-2007 05:42 AM
From File to Array Rock Shell Programming and Scripting 2 02-08-2007 02:10 AM
Dump an array in a file IMD Shell Programming and Scripting 3 08-31-2006 07:04 AM
How can i read array elements dynamically in bash? haisubbu UNIX for Dummies Questions & Answers 1 08-28-2006 11:19 PM

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

Join Date: Jul 2007
Posts: 33
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
How to read from txt file and use that as an array

Hi Guys

How u all doing?

I am having tough time to achieve this I have a unix .ksh script which calls
sql script

Right now I harcoded column id's in sql script but I want to read them from a txt file
1084,1143,1074,1080,1091,1090,1101,1069,1104,1087,1089,1081

I want to read this file as an array in my sql script


My ksh script is



#!/usr/bin/ksh
DIRNAME=`dirname $0`
# set up environment for oracle based SMO database instance
. $DIRNAME/defaults_env.ksh

echo "Enter the region (ie. 001 or 002): "
read sdzone;
export userFile="cvsusers.txt"
LOGFILE=$DIRNAME/cvs_users_defaults.log
TIME=`date`

#checks to see if the log files is present, if not then creates it
if [ -a $DIRNAME/cvs_users_defaults.log ]
then
:
else
touch $DIRNAME/cvs_users_defaults.log
fi

#Checking to see if a data file(cvsusers.txt) exists for a load.
if [ -a $DIRNAME/$userFile ]
then
# The list of userIds is supplied from userId file


INFILE=$userFile;
echo "Using file $INFILE as input for user id";
echo " ";
echo "Started Process on $TIME" >> $LOGFILE
echo "Calling procedure to initialize users"
for userId in `cat $INFILE`
do
echo "Storing User Defaults for User " $userId >> $LOGFILE
userIdLength=`expr length $userId`
if [ $userIdLength -gt 8 ]
then
echo "Length of UserID " $userId " is too long">> $LOGFILE
else
echo "connecting to SQL">> $LOGFILE

if [[ $sdzone = '001' ]]; then
sqlplus -s $ZONE1_USERNAME_525/$ZONE1_PASSWRD_525@$ZONE1_DBNAME_525 \@$DIRNAME/sql/cvs_users_defaults.sql $userId >> LOGFILE
echo "connected to SQL">> $LOGFILE


elif [[ $sdzone = '002' ]]; then
sqlplus -s $ZONE2_USERNAME_525/$ZONE2_PASSWRD_525@$ZONE2_DBNAME_525 \@$DIRNAME/sql/cvs_users_defaults.sql $userId >> LOGFILE
echo "connected to SQL">> $LOGFILE


else
echo "Invalid region code!"
exit;
fi

fi
done


#rename the file after processing
logtime=`date "+%y%m%d%H%M%S"`
newUserFile="cvs_users_processed_"$logtime".txt"
mv $userFile $newUserFile

echo "Ending process of setting user defaults at $TIME " >> $LOGFILE
echo "Check the log file at $LOGFILE"
exit 0
else
#IF "custsoncologyusers.txt" file does not exist in DIRNAME then end gracefully
echo "No user default file ($userFile) is available to load"
echo "No user default file ($userFile) is available to load" >> $LOGFILE
echo "Ending user default process with a return code 0 at $TIME" >> $LOGFILE
exit 0
fi


This is my sql script( cvs_users_defaults.sql)

WHENEVER SQLERROR EXIT SQL.SQLCODE;
set serveroutput on;
set verify off;
set feedback off;

declare
userId s_user_cust_acct.user_id%TYPE := '&1';
TYPE t_col_id_tab IS TABLE OF number;
my_col_id t_col_id_tab;
TYPE t_col_seq_num_tab IS TABLE OF number;
my_col_seq_num t_col_seq_num_tab;
TYPE t_all_col_id_tab IS TABLE OF number;
all_col_id t_all_col_id_tab;
tcount number;
begin
select count(*)
into tcount
from s_column_atrb
where user_id = UPPER(userId)
and screen_typ_cd = 0;
if (tcount > 0) THEN
dbms_output.put_line('updating s_column_atrb' || userId);

-- For Orders(I hard coded the array here but I want to read this from .ksh script by feeding it with a txt file)
all_col_id := t_all_col_id_tab(1069,1071,1072,1073,1074,1075,1076,1077,1078,1080,1081,1082,1083,1084,1085,1086,108 7,1088,1089,1090,1091,1101,1102,1103,1104,1114,1115,1116,1117,1118,1119,1120,1122,1123,1124,1125,112 6,1127,1128,1129,1130,1131,1132,1133,1134,1135,1136,1137,1138,1139,1140,1141,1142,1143,1144,1145,114 6,1147,1148,1149,1150,1151,1152,1154,1169,1178,1182,1183,1195);
my_col_id := t_col_id_tab(1084,1143,1074,1080,1091,1090,1101,1069,1104,1087,1089,1081);

my_col_seq_num := t_col_seq_num_tab(4,12,2,6,7,8,10,1,9,3,5,11);

For i in 1..12 LOOP
dbms_output.put_line('my_col_id(' || i || ') is ' || my_col_id(i));
update s_column_atrb
set show_hide_ind = 'Y', column_seq_num = my_col_seq_num(i)
where user_id = UPPER(userId)
and screen_typ_cd = 0
and col_id = my_col_id(i);
END LOOP;
end;
/
exit


can any body help to get this done
Thanks in advance
Pinky
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-07-2007
Moderator
 

Join Date: Dec 2003
Location: /ksh93
Posts: 716
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
The simplest way would be to build "cvs_users_defaults.sql" on the fly before
you execute it.

For example: the file "head" would contain

Code:
WHENEVER SQLERROR EXIT SQL.SQLCODE;
set serveroutput on;
set verify off;
set feedback off;

declare
userId s_user_cust_acct.user_id%TYPE := '&1';
TYPE t_col_id_tab IS TABLE OF number;
my_col_id t_col_id_tab;
TYPE t_col_seq_num_tab IS TABLE OF number;
my_col_seq_num t_col_seq_num_tab;
TYPE t_all_col_id_tab IS TABLE OF number;
all_col_id t_all_col_id_tab;
tcount number;
begin
select count(*)
into tcount
from s_column_atrb
where user_id = UPPER(userId)
and screen_typ_cd = 0;
if (tcount > 0) THEN
dbms_output.put_line('updating s_column_atrb' || userId);
The file "tail" would contain:

Code:
my_col_seq_num := t_col_seq_num_tab(4,12,2,6,7,8,10,1,9,3,5,11); 

For i in 1..12 LOOP
dbms_output.put_line('my_col_id(' || i || ') is ' || my_col_id(i));
update s_column_atrb
set show_hide_ind = 'Y', column_seq_num = my_col_seq_num(i)
where user_id = UPPER(userId)
and screen_typ_cd = 0
and col_id = my_col_id(i);
END LOOP;
end;
/
exit
and the file "orders" would contain the "modifiable code"

Code:
-- Orders
all_col_id := t_all_col_id_tab(1069,1071,1072,1073,1074,1075,1076,1077,1078,1080,1081,1082,1083,1084,1085,1086,108 7,1088,1089,1090,1091,1101,1102,1103,1104,1114,1115,1116,1117,1118,1119,1120,1122,1123,1124,1125,112 6,1127,1128,1129,1130,1131,1132,1133,1134,1135,1136,1137,1138,1139,1140,1141,1142,1143,1144,1145,114 6,1147,1148,1149,1150,1151,1152,1154,1169,1178,1182,1183,1195);
my_col_id := t_col_id_tab(1084,1143,1074,1080,1091,1090,1101,1069,1104,1087,1089,1081);
You would then modify your shell script to build the sql script as follows:

cat head orders tail > cvs_users_defaults.sql

before invoking cvs_users_defaults.sql
Reply With Quote
  #3 (permalink)  
Old 10-07-2007
Registered User
 

Join Date: Jul 2007
Posts: 33
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
I appreciate your help but I totally got confused and posted a new thread related to this
Thanks for answering
Reply With Quote
  #4 (permalink)  
Old 10-07-2007
Registered User
 

Join Date: Jul 2007
Posts: 33
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
hey Guys

I wanted to ask my question in detail what I wanted to achieve is

I have txt file
which looks like this

x.txt
5
8
10
15
17
19

I have abc.ksh and xyz.sql scripts

I want to read x.txt file into an array in abc.ksh (shell script) and pass that array to xyz.sql (sql script) where I wanted to read the values of that array and use those values in logic in xyz.sql (sql script).


Thanks
Apprciate any help
I am desperate and needed help
Pinky
Reply With Quote
  #5 (permalink)  
Old 10-07-2007
Registered User
 

Join Date: Jul 2007
Posts: 33
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Hey fpmurphy

if I want to build my .sql file on the fly do I have to save the head and tail and orders as head.sql , tail.sql , and orders.sql
can u plz elaborate

I am just wondering whether there is a way that we can send arrays as parameters from shell script to sql script

Thanks a lot for all ur help
Pinky
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:48 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