Solved

Problem With Script Output

Posted on 2011-09-16
10
320 Views
Last Modified: 2012-06-27
Hi Experts...
I have a script which has isql code in it which on passing values gives a studentId and a count as the output.
I write this output to a file and read the output from a java program.
In java program i pass a list of student information in a for loop to this script so that the sudents info is added to the file one after the other.
The problem is i have the student information pertaining to the last studentId passed to the script.
The previous ones are overwrittem by the new ones.
How do i have the info written to the file one after the other in new lines.
The script code is given below.
Please help...
#!/usr/bin/tcsh

# Checking the prameters
if ( $# != 2 ) then
    echo "Invalid Parameter"
    exit -1
endif

set studentId="$1"
set fileName="$2"

isql -S$SQL_SRVR -D$DB_NAME -U$SYB_LOAD_ID -P$SYB_LOAD_PWD -b -w3000 -n -h0 -o $fileName <<!

set nocount on
go

select rtr.studentId,count (*)
from StudentTrackingRequest rtr, Attendence mtp, RollDistribution rd 
where  rtr.clientID = "$1" 
and rd.clientID = rtr.clientID  
and mtp.rtvStatusCode = 'VAL'  
and rd.creationDateTime > 'Sep  7 2011  9:02:29:000AM' 
and  rd.atendenceDate >= '08/31/11' 
and  rd.atendenceDate <= '09/08/11'
group by rtr.clientID
    
go
!
exit 0

Open in new window

0
Comment
Question by:gaugeta
  • 6
  • 4
10 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36547871
Sorry, it is hard to understand - are you asking how to append to the file?
If so, do you want to append using the script or the java program?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36547938

In java to open file for appending you need to create FileOutputStream using
boolean true in the constructor:

new FileOutputStream(File file, boolean append)

if append is true file will be opened for appanding

In unix script
if you write to the file using
>>

instaed of

>

you'll be appending to the file rather than writing from the beginning
0
 

Author Comment

by:gaugeta
ID: 36547941
@for_yan:I want to output to be appended to the file and not erasing the previous existing data.
Output format:
Jake 300
Sid 500
Either way it could be appended using java code or script whichever works.
Please help...
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:gaugeta
ID: 36547957
@for_yan:So where do i change the script for the appending to happen.
Please help...
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36547972
In the java program this is an example for appending to file:


http://www.devdaily.com/java/edu/qanda/pjqa00009.shtml
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36547980


In the script you posted I don't see any writing to the file
but in  general in unix script

if you write usiing >> you'll be appending

for expample:

echo $PATH >> output.txt

echo $CLASSPATH >> output.txt

in the output.txt you'll first see the contents of your PATH
and then the contents of your CLASSPATH environemn variables

So in the first case it will open a new file (as there is no file prsent)
in the second case it will open file , find the end of it
and pappend the second output
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36547988

In the Java example form the link abovr
it is second parameter true in this constructor:
 bw = new BufferedWriter(new FileWriter("checkbook.dat", true));

which ensures that the output is appended
to the existing file


public void appendToCheckbook () {

      BufferedWriter bw = null;

      try {
         bw = new BufferedWriter(new FileWriter("checkbook.dat", true));
	 bw.write("400:08311998:Inprise Corporation:249.95");
	 bw.newLine();
	 bw.flush();
      } catch (IOException ioe) {
	 ioe.printStackTrace();
      } finally {                       // always close the file
	 if (bw != null) try {
	    bw.close();
	 } catch (IOException ioe2) {
	    // just ignore it
	 }
      } // end try/catch/finally

   } // end test()

Open in new window

0
 

Author Comment

by:gaugeta
ID: 36548011
@for_yan:Thanks for the reply.
Here i realised i cannot append to the file using java code sicnce it does not have data to append.

Here data is got from script.
Here i script what ever is the result of the isql code between ! and ! is written to the file.
I see that data is written to the file as there is data of the last student as in this case all data of previous students have been overwritten.
Please help...
isql -S$SQL_SRVR -D$DB_NAME -U$SYB_LOAD_ID -P$SYB_LOAD_PWD -b -w3000 -n -h0 -o $fileName [From here]<<!

set nocount on
go

select rtr.studentId,count (*)
from StudentTrackingRequest rtr, Attendence mtp, RollDistribution rd 
where  rtr.clientID = "$1" 
and rd.clientID = rtr.clientID  
and mtp.rtvStatusCode = 'VAL'  
and rd.creationDateTime > 'Sep  7 2011  9:02:29:000AM' 
and  rd.atendenceDate >= '08/31/11' 
and  rd.atendenceDate <= '09/08/11'
group by rtr.clientID
    
go
![Till Here]

Open in new window

0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36548040
If you don't write -o $filename in the command line - I guess I would just write the result to standars oiutput - check if it is so

If it is so then instead of -o $filename
Write
 >> filename.txt

I'm not sure where to put it in this way
Perhaps after the input - after second exclamation point - try bot ways
If it writes outpit t standout then redirectio should work
0
 

Author Comment

by:gaugeta
ID: 36548075
@for_yan:Thanks a lot it works.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question