The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Conditionally joining lines in vi ifermon UNIX for Dummies Questions & Answers 0 06-04-2008 06:43 AM
search for the contents in many file and print that file using shell script cdfd123 Shell Programming and Scripting 3 10-07-2007 07:17 PM
sed csv remove conditionally Jae Shell Programming and Scripting 6 08-03-2007 12:29 PM
Email from script conditionally dfb500 Shell Programming and Scripting 2 05-01-2007 10:01 AM
Creating file contents using contents of another file ReV Shell Programming and Scripting 21 02-24-2006 07:25 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 10-13-2005
Registered User
 

Join Date: Feb 2005
Location: Bangalore
Posts: 36
How to update the contents in a file conditionally?

Hi All,

I have a data file which has two columns Location and the Count.

The file looks like this
India 1
US 0
UK 2
China 0

What I have to do is whenever I fails to login to Oracle then I have to add 1 to the count for that location.

Whenever my script fails to login to Oracle for a particular location, the script needs to find the record for that location and then change the count. I should not change the count for all the locations, instead I need to change the count only for that location.

Ex: Suppose if I am trying to login to Oracle for India location and fails to login then I have to update the count to 2.

Your help will be greatly appreciated.

Thanks in Advance
Raju
Reply With Quote
Forum Sponsor
  #2  
Old 10-13-2005
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,328
Try this:

Code:
#!/usr/bin/ksh

while read country count; do
   ### try to login to oracle and set some error condition
   if [ #the failure condition is set ]; then
      count=$(($count+1))
   fi
   echo $country $count >> your_file.tmp
done < your_file
mv your_file.tmp your_file
Some of the script is not real code, because I am not sure how you are going to check if you can connect to the database.
Reply With Quote
  #3  
Old 10-13-2005
Registered User
 

Join Date: Feb 2005
Location: Bangalore
Posts: 36
It will just append a line, what I want is I have to update that record only.

And one more thing is, how to get that count for a particular location.

Thanks for your immediate responce.

Raju
Reply With Quote
  #4  
Old 10-13-2005
Neo's Avatar
Neo Neo is offline
Administrator
 

Join Date: Sep 2000
Location: Asia Pacific
Posts: 4,493
There are many ways to do this.

One way to do it is to write a temporary file in a directory each time a login fails. For example, your directory might look like:

india.pid1
us.pid2
uk.pid3

As you can see you can append the process id to the origins to avoid filename collisions.

Then, in a cron job, you can read the file names and add them up for each origin. Then, read the flat file where the previous values are stored into a array and add the new and old values, and write the new file.

Make sure to clean up the directory of temporary files.

You can also do this without an intermediate temporary file used as an IPC, in a single process, but if you have multiple login failures at one time, you will have have to implement file locking, which might be slower and more complex than a simple IPC like a temporary file.

There are myriad ways to do this. I suggest you use an intermediate approach like I suggested to avoid problems if you get multiple login failures in bursts - having said that, you did not describe the software architecture where the actually login failure flag (detection) occurs.
Reply With Quote
  #5  
Old 10-13-2005
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,328
Quote:
Originally Posted by rajus19
It will just append a line, what I want is I have to update that record only.

And one more thing is, how to get that count for a particular location.

Thanks for your immediate responce.

Raju
Observe the code closely. I am writing to a temporary file and moving that file on to the original. i.e. If you fail to login to India, then I write 'India 2' to the temp file. Then I try to login to US. If that succeeds, then I will be writing 'US 0' (the line that I read) to the file. After the while loop is complete the temp file has the correct information about the login failures. So I move the temp file onto your original file.
Reply With Quote
  #6  
Old 10-14-2005
Registered User
 

Join Date: Feb 2005
Location: Bangalore
Posts: 36
Thank you very much.

Regards,
Raju
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:08 AM.


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