Solved

Problem With Script Output

Posted on 2011-09-16
10
333 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

623 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