使用java中的参数执行shell脚本
我一直在谷歌搜索,每个人似乎有一个不同的解决方案,其中没有一个似乎对我有用。
我已经尝试过ProcessBuilder
和Runtime
。 两者都直接调用.sh
文件并将其提供给/bin/bash
。 没有运气。
回到基础,我目前的代码如下;
String cmd[] = { "~/path/to/shellscript.sh", "foo", "bar" }; Process p = Runtime.getRuntime().exec(cmd);
尽管手动运行,但是No such file or directory
错误;
~/path/to/shellscript.sh foo bar
从bash完美运作。
我需要保留~
因为这个shellcript以三种不同用户的略有不同的forms存在。
一种选择是自己处理:
String homeDir = System.getenv("HOME"); String[] cmd = { homeDir + "/path/to/shellscript.sh", "foo", "bar" }; Process p = Runtime.getRuntime().exec(cmd);
另一个是让Bash为你处理它:
String[] cmd = { "bash", "-c", "~/path/to/shellscript.sh foo bar" }; Process p = Runtime.getRuntime().exec(cmd);
如前所述,tilde是一个特定于shell的扩展,应该通过将其替换为当前用户的主目录来手动处理(例如,如果定义了$HOME
)。
除了已经给出的解决方案,您还可以考虑使用Apache Commons项目中的commons-io和commons-exec :
... import org.apache.commons.exec.CommandLine; import org.apache.commons.exec.DefaultExecutor; import org.apache.commons.exec.Executor; import org.apache.commons.io.FileUtils; ... CommandLine cmd = new CommandLine("path/to/shellscript.sh"); cmd.addArgument("foo"); cmd.addArgument("bar"); Executor exec = new DefaultExecutor(); exec.setWorkingDirectory(FileUtils.getUserDirectory()); exec.execute(cmd); ...
一般来说,我建议您使用ScriptEngine而不是System.getRuntime()。exec
我认为它会让事情变得更容易。
请记住,你需要这个JDK 6及以上版本。
此外,关于您的具体问题 – 我真的认为这个问题应该是可配置的。
您可以执行以下操作:
A.在.bash_rc或.bash_profile中(对于每个用户)定义配置的路径
脚本使用:
出口MY_SCRIPT =
B.使用,从java代码中读取此值
String sciprtPath = System.getenv("MY_SCRIPT")
获取值。
C.运行脚本,在代码中执行的方式,使用scriptPath变量或使用scriptEngine。