Solved

Problem With Script Output

Posted on 2011-09-16
10
305 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
 

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

760 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now