file is getting overwritten


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting file is getting overwritten
# 1  
Old 08-18-2010
file is getting overwritten

Hello All,

I am writing a bash script on Solaris O/S. I looping through an array. For each iteration, i connect to the datatabase and use select statement. Output of which is redirected to .CSV file. here is the code for it.
Code:
output="loop.csv"

elements=${#currency_pair[@]}

for((i=0;i<$elements;i++)); do
  echo ${currency_pair[${i}]}
  results=`isql -U$DBLOGIN -P$DBPASSWD -D$DBNAME -b -w 2000 -s',' - o$output <<EOF
 select Code,BidRate,AskRate from FXPoint where RelativeDateType = 1  and
(Code = '${currency_pair[${i}]}')
go
EOF`
done

But the problem is that .CSV file is getting overwritten because, for each iteration of the loop, database connection is getting opened and closed.

How to modify this code to
1) Open the database connection only once.
2) Loop through entire array. Pass the select statement output to the .CSV file
3) close database connection.

Any help is appreciated.

Thanks & Regards
Arundhati

Last edited by jim mcnamara; 08-18-2010 at 07:44 AM.. Reason: code tags please
# 2  
Old 08-18-2010
Hi Arun,

It can be done with a small change in logic.

Code:
output="loop.csv"
tempfile="loop.tmp"
>$output
elements=${#currency_pair[@]}

for((i=0;i<$elements;i++)); do
  echo ${currency_pair[${i}]}
  results=`isql -U$DBLOGIN -P$DBPASSWD -D$DBNAME -b -w 2000 -s',' - o$tempfile <<EOF
 select Code,BidRate,AskRate from FXPoint where RelativeDateType = 1  and
(Code = '${currency_pair[${i}]}')
go
EOF`
cat $tempfile >> $output
rm -f $tempfile
done

Regards,

Ranjith
# 3  
Old 08-18-2010
How about:
Code:
output="loop.csv"

elements=${#currency_pair[@]}
for((i=0;i<$elements;i++)); do
  if [ "${i}" -eq 0 ]; then
     SELECT="('${currency_pair[${i}]}'"
  else
     SELECT="${SELECT},'${currency_pair[${i}]}'"
  fi
done
SELECT="${SELECT})"

results=`isql -U$DBLOGIN -P$DBPASSWD -D$DBNAME -b -w 2000 -s',' - o$output <<EOF
 select Code,BidRate,AskRate from FXPoint where RelativeDateType = 1  and
 Code in ${SELECT}
go
EOF`

# 4  
Old 08-18-2010
Thanks Ranjithpr and ctaylor.

I find the second suggestion better because it executes the query in one go. Thanks a lot for your help Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Ssh2 key has been overwritten, need a way to restore

I had generated a ssh2 key on my AIX box, to receive files from other AIX and Linux systems. Key Name: id_ssh2_server.pub However this ssh2 key (both public and private keys) has been overwritten, while I was generating another ssh2 key. Now the earlier configured target systems are not able... (3 Replies)
Discussion started by: freakygs
3 Replies

2. Shell Programming and Scripting

How to preserve the value of a variable from being overwritten?

Hi All, I am new new to unix.com, I have a question related to shell scripting. We have a Oracle database backup shell script, which can be used for taking full, incremental & archive log backup based on the parameters passed. Within the script we export a variable as export... (5 Replies)
Discussion started by: veeresh_15
5 Replies

3. Programming

variables overwritten

Hi, i have some problems with the following code: char *tab_path; char *sep=" \t\n"; char line; char *p; FILE * file; int i = 0; if(fgets(line,MAXLINE,file)!=NULL){ if((p=strtok(line,sep))!=NULL)tab_path=p; while((p=strtok(NULL,sep))!=NULL){ i++; ... (4 Replies)
Discussion started by: littleboyblu
4 Replies

4. Solaris

overwritten rootdisk?

Hi, The dump device on my system was set to /dev/dsk/c0t0d0s7. I have done a savecore -Lv on the system which worked fine. I'm wondering have I overwritten the rootdisk here by mistake? The system is still up but will need to be rebooted due to an error on it. Will it come back up? ... (8 Replies)
Discussion started by: gwhelan
8 Replies

5. UNIX for Advanced & Expert Users

Data Recovery from file system overwritten with LVM.

Hey peeps, Here is somethin u might find interestin.... Is it possible to recover data from a partition which used to be an ext3 file sytem with some nice forgotten backups, which now is an lvm partion containg root partition of another OS. :) I couldn't create any mess better than this, can... (2 Replies)
Discussion started by: squid04
2 Replies

6. HP-UX

Critical files in /etc overwritten EMPTY!

The following files were wiped out - new empty files were left in their place. /etc/inittab, /etc/inetd.conf, and /etc/MANPATH The system is running HP-UX 11i v3 - Mar08. Anyone seen anything like this? Any ideas on a way to figure this out if it happens again or a suggested way to... (9 Replies)
Discussion started by: KEnglander
9 Replies

7. SCO

Overwritten /dev/root -recovery of SCO OpenServer 5.0.4

Due to my own stupidity I managed to overwrite my /dev/root device using dd (don't ask). Current state is - Have current backup created using cpio (command used was 'find . -mount -depth -print|cpio -ocB > $TAPE') - Once I realised what was happening powered off the server but this was too... (2 Replies)
Discussion started by: ok2
2 Replies

8. AIX

UIDs being overwritten immediately

We have a problem where we delete a user and their associated UID gets dumped back in the UID pool. The if we immediately create a another (new) user, AIX reuses the last UID, the one that was just released. This is causing a problem when reports are being generated because the new users name is... (2 Replies)
Discussion started by: xsys2000
2 Replies

9. UNIX for Dummies Questions & Answers

Grub Loader entry overwritten

Hello, One of my frend had a problem. He had Windows XP installed on his system. Then he installed Red Hat Linux 8.0 in one of the partitions. After some time his XP got corrupt and then he reinstalled Windows XP. This over wrote the Grub loader entry, and due to this the grub loader is not... (2 Replies)
Discussion started by: rahulrathod
2 Replies

10. UNIX for Advanced & Expert Users

.cshrc and .login overwritten !!

Hi, My account is : abcd I belong to a group: pqrs Some thing straneg happened yesterday. My .cshrc and .login got overwritten into pqrs's .cshrc and .login I obviously did not explicitly overwrite pqrs's .cshrc. Are there any reasons how this could have happened indirectly due to... (5 Replies)
Discussion started by: gjthomas
5 Replies
Login or Register to Ask a Question