zeroturnaround / zt-exec

ZeroTurnaround Process Executor

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

bash script support

yyLelouch opened this issue · comments

I get following error message:

19:47:50.615 [main] DEBUG org.zeroturnaround.exec.ProcessExecutor - Started java.lang.UNIXProcess@246ae04d
19:47:50.620 [WaitForProcess-java.lang.UNIXProcess@246ae04d] DEBUG org.zeroturnaround.exec.WaitForProcess - java.lang.UNIXProcess@246ae04d stopped with exit code 127
19:47:50.622 [main] ERROR Exit code: 127 Output: /bin/sh: source /$path/test.sh&&testFunc: No such file or directory

part of the backtrace message:

org.zeroturnaround.exec.InvalidExitValueException: Unexpected exit value: 127, allowed exit values: [0], executed command [/bin/sh, -c, "source /$Path/test.sh&&testFunc"]

The command /bin/sh -c "source /$Path/test.sh&&testFunc" runs perfectly on my box.

Here is my definition of the ProcessExecutor:

final ProcessExecutor processExecutor = new ProcessExecutor()
                .command("/bin/sh", "-c", "\"source /$Path/test.sh&&testFunc\"")
                .exitValueNormal()
                .timeout(5, TimeUnit.SECONDS)
                .readOutput(true);

It seems ProcessExecutor doesn't take the "-c" option.

I resolve this issue by removing the quotation marks.

final ProcessExecutor processExecutor = new ProcessExecutor()
                .command("/bin/sh", "-c", "source /$Path/test.sh&&testFunc")
                .exitValueNormal()
                .timeout(5, TimeUnit.SECONDS)
                .readOutput(true);

According to my understanding, bash -c need to read from string. In our shell, string format needs the quotation marks. Following are from man bash:

OPTIONS
       In addition to the single-character shell options documented in the description of the set builtin command, bash interprets the following options when it is invoked:

       -c string If the -c option is present, then commands are read from string.  If there are arguments after the string, they are assigned to the positional parameters, starting with $0.

The ProcessExecutor.command can take String... command. Therefore, no need to use quotation marks inside the string.