Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problem With Script Output

Posted on 2011-09-16
10
Medium Priority
?
350 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
Independent Software Vendors: 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 2000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

926 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