I have a .sh
file stored in some Linux system. The full path of the file is:
/comviva/CPP/Kokila/TransactionHandler/scripts/stopTH.sh
>
Here is my code. As the comment says, works on Linux, fails on Windows (XP). AFAIK the problem with Windows is that cmd.exe is weird regarding it's parameters. For your specific sub-task you probably can get it to work by playing with quotes and maybe embedding the sub-task parameters in the subtask itself.
/** Execute an abritrary shell command.
* returns the output as a String.
* Works on Linux, fails on Windows,
* not yet sure about OS X.
*/
public static String ExecuteCommand( final String Cmd ) {
boolean DB = false ;
if ( DB ) {
Debug.Log( "*** Misc.ExecuteCommand() ***" );
Debug.Log( "--- Cmd", Cmd );
}
String Output = "";
String ELabel = "";
String[] Command = new String[3];
if ( Misc.OSName().equals( "WINDOWS" ) ) {
Command[0] = System.getenv( "ComSPec" );
Command[1] = "/C";
} else {
Command[0] = "/bin/bash";
Command[1] = "-c";
}
Command[2] = Cmd;
if (DB ) {
Debug.Log( "--- Command", Command );
}
if ( Misc.OSName().equals( "WINDOWS" ) ) {
Debug.Log( "This is WINDOWS; I give up" );
return "";
}
try {
ELabel = "new ProcessBuilder()";
ProcessBuilder pb = new ProcessBuilder( Command );
ELabel = "redirectErrorStream()";
pb.redirectErrorStream( true );
ELabel = "pb.start()";
Process p = pb.start();
ELabel = "p.getInputStream()";
InputStream pout = p.getInputStream();
ELabel = "p.waitFor()";
int ExitCode = p.waitFor();
int Avail;
while ( true ) {
ELabel = "pout.available()";
if ( pout.available() <= 0 ) {
break;
}
ELabel = "pout.read()";
char inch = (char) pout.read();
Output = Output + inch;
}
ELabel = "pout.close()";
pout.close();
} catch ( Exception e ) {
Debug.Log( ELabel, e );
}
if ( DB ) {
Debug.Log( "--- Misc.ExecuteCommand() finished" );
}
return Output;
}
}