Search code examples
javashellkshprocessbuilder

How does the final Processbuilder command look like?


I am running a shell script(ksh with shebang at top of script) from my java program using ProcessBuilder:

processBuilder.command("/bin/sh","-c"," . " + /somepath/script.ksh + " " + argument);
proc = processBuilder.start();

Everything works fine using my java program. I wanted to run the command on the command line and figured the command being run from the java program was:

/bin/sh -c . /somepath/script.ksh argument

However, that does not work on the command line and I receive this error:

/somepath/script.ksh: line 0: .: filename argument required
.: usage: . filename [arguments]

Looks like the "-c" flag is expecting the next item to be a command(which in my statement is the source operator "."), and after that it is expecting more arguments. So my script(/somepath/script.ksh) is being taken as argument instead of a command. Why does it work with processbuilder in the java program? Is it creating the command differently?


Solution

  • The -c option to /bin/sh would make sh interpret the argument as a command line. So instead of saying:

    /bin/sh -c . /somepath/script.ksh argument
    

    you'd need to say:

    /bin/sh -c ". /somepath/script.ksh argument"
    

    Alternatively, you could say:

    /bin/sh /somepath/script.ksh argument