Solved

Problem With Script Output

Posted on 2011-09-16
10
324 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
Industry Leaders: 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

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!

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

762 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