0

I am currently executing a csh shell script from a GUI button. The script originally calls another sql script to select information from a database and output it to a log file in the tmp directory. I now need to CAT that file and display it on the xterm window. I use the java Runtime.exec() method to execute the command and I figured I have two options that I can use

  1. Redirect the std output to show it on the xterm
  2. Read from the log file using the csh script and ouytput to xterm

So I chose option 2 and I cannot even get the csh script to output a simple hello world which means I cannot out put anything to the screen when I run it from netbeans. When I try to run the script directly from the xterm it works . Here is the script code

#!/bin/csh -f



#set MYSQL=${MYSQL_HOME}/mysql
#set PSQL=${PSQL_HOME}/psql

echo "Hello World"

#set      REPORT=${CLEADM_HOME}/Scripts/DataValidation/CLEADM_EarthOrientationParametersDataReport.sql


#${REPORT}

#${PSQL} ${CLEDBUSER}<${REPORT} 

#Get the number of lines in the report file for scrollbar control
#set lc='wc -l /tmp/results.log'
#echo $lc

I commented everything out just to see if I could even print (echo) the results to the screen and that doesn't work. When I run it from the xterm directly (~/.mysqlconnection.csh) it seems to work and prints out "Hello World". I do not know what is wrong. So i tried to force the terminal to come up by adding this code at the top of my script

/usr/openwin/bin/XTERM \    this opens up the xterm but leaves it frozen for some reason? now i cant access anything on my computer

below is the code i use to exeucte the csh script from a java button. i call this method behond the button

public class RunShellScript {

public static void runShellScript (String unixCommand) 
{
 try {
     Runtime runtime=Runtime.getRuntime();
     Process process=runtime.exec(new String [] { "/bin/csh", "-c", unixCommand});

     InputStream stderr=process.getErrorStream();
     InputStreamReader isr=new InputStreamReader (stderr);
     BufferedReader br=new BufferedReader (isr);
     String line=null;
     System.out.println("<ERROR>");


     while((line=br.readLine())!=null)
         System.out.println(line);

     System.out.println(line);
     int exitVal=process.waitFor();
     System.out.println("Process exitValue:" + exitVal);
 }
 catch (Throwable t)
 {
     t.printStackTrace();
 }

somewhere i need to redirect the output to an exterm and i am confused as to how to dop that using the streamreader

rambokayambo
  • 341
  • 4
  • 10
  • 27

1 Answers1

1

EDIT - I'm completely starting over with my answer since I badly misunderstood the requirements.

If you want a new window each time the user presses the button, then opening an xterm from the csh script should work; try this:

/usr/openwin/bin/xterm -e "bash -c 'cat /tmp/results.log; echo press a key to continue; read'" &

If you want one window that stays open forever, but keeps updating with new results each time the user presses the button, that's a little different. I would try spawning the other window from Java. You could either have a separate Java window/frame, or use another xterm. To use another xterm, try running a Process somehow, like this:

new ProcessBuilder("/usr/openwin/bin/xterm","-e","tail -f /tmp/results.log").start();

That will open up the tail -f command on the results.log file, and everything you add to the log file will show up in the xterm.

Rob I
  • 5,627
  • 2
  • 21
  • 28
  • im sorry im new to java and i dont see how your example will fit my situation. Do you want me to post the code of the class and method i am using to call the script sing java runtime.exec? – rambokayambo Aug 06 '12 at 16:55
  • your exqample is good but it seems to only take care of the error output. I also want to the actual output to be printed to the xterm and i dont see it anywhere in your code? – rambokayambo Aug 06 '12 at 17:18
  • The `pb.redirectErrorStream()` call should cause the error output to be sent to the same stream as the standard output, so that both are accessible with `getInputStream()`. – Rob I Aug 06 '12 at 17:41
  • where do you specify the standard output to go to the xterm screen? – rambokayambo Aug 06 '12 at 17:54
  • Oh. I think I finally understand (sorry!). Would a simple solution like running `tail -f /tmp/results.log` from the xterm work for you? Since your script already seems to update that file, that seems clean enough. All that Java stuff I added would not be necessary. – Rob I Aug 06 '12 at 18:03
  • sorry maybe i did not explain it well. My script outpouts the data to a log file and now i want to cat that log file onyyo the xterm. Would i have to insert that syntax in into the csh file? – rambokayambo Aug 06 '12 at 18:07
  • No, the csh file should be fine. Just run that command from the prompt in the xterm. The xterm is running on the same node as your java/csh tool, correct? – Rob I Aug 06 '12 at 18:09
  • yes..but i think you still misunderstand. The output should be invoked on the xterm when the user presses a button or an event on the java GUI and NOT by namually running the command from the prompt – rambokayambo Aug 06 '12 at 18:21
  • Unfortunately, if you want something output in the xterm, you have to run a command in the xterm. The `tail -f` command can be run once, when you first open the xterm (or open the xterm like `xterm -e "tail -f /tmp/results.log" &`), and continue to see the changes to the log file each time it's updated from button presses. Another solution would be to not use an xterm, but rather open another Java window. – Rob I Aug 06 '12 at 18:26
  • well my requirements specify this should be how my output is viewed and so this is what i must use. I think it canbe done because when i put the following in my csh shell scrip /usr/openwin/bin/xterm \ then is actually opens up another xterm but then i t leaves my screens frozen – rambokayambo Aug 06 '12 at 18:33
  • i reposted it in my original querstionfor you to see my edit of the script – rambokayambo Aug 06 '12 at 19:01