Re: redirecting stderr & stdout using exec

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Re: redirecting stderr & stdout using exec

Carlos Hernandez-2
Aiman Younis <beanshell@...> writes:

>
> Hi folks.
>
> Just started using BeanShell. Nice job :)
>
> One thing I'm trying to do within my script is to run an application using
> exec(), and have that application's stdout and stderr go to a file rather
> than the console.
>
> I.e., when I run it from the command line (in Linux & WinXP):
> myProg myArg >stdout.txt 2>stderr.txt
> everything is fine.
>
> But when I put that in the exec() in my BeanShell script, it
> (understandably) takes the ">stdout.txt" and the "2>stderr.txt" as arguments
> 2 and 3.
>
> So my question is, does BeanShell's exec() command (or any other command)
> have any simple way of redirecting output? Or will I have to find a (more
> complicated) way on my own?
>
> I did a little searching through the list archives. I've seen that you can
> get a Process back from an exec() call, but couldn't find if I could do
> anything with that Process to enable the redirecting.
>
> Thanks,
> Aiman
>
> -------------------------------------------------------
> This SF.Net email is sponsored by: IBM Linux Tutorials
> Free Linux tutorial presented by Daniel Robbins, President and CEO of
> GenToo technologies. Learn everything from fundamentals to system
> administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
>


Yes, you can redirect stdout through BeanShell's output stream using the method
setOut() from this.interpreter variable, e.g.:

-------------------
// Output will go to a file
f = new FileOutputStream("Report.txt");
// Initialize a PrintStream with the FileOutputStream above
p = new PrintStream(f);
// Redirect bsh output
this.interpreter.setOut(p);

// Execute MySQL query and send to file
exec("mysql -u root -pXXXXX -h host -D mydatabase -e \"SELECT * FROM foo;"\");
-------------------

Works fine on Windows, but in Linux has no output. Commands such as "ls -l"
gives me output in BeanShell but a mysql command as the above one not.

Strangely, "mysql --help" gives me output. I think is the way Linux commands
uses stdin and stdout (?)

At the moment, I don't know how to redirect to stderr.
 



Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users