Java ProcessBuilder和bash [英] Java ProcessBuilder and bash
本文介绍了Java ProcessBuilder和bash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 ProcessBuilder
从Java执行bash脚本我的代码是:
I am trying to execute a bash script from Java with ProcessBuilder
my code is :
Process createUser = buildProcess(
"/bin/su",
"-c",
"\"/opt/somedir/testdir/current/bin/psql",
"--command",
commandForUserCreation,
/* "'select * from users'", */
"--dbname",
"mydbname\"",
"myuser"
);
问题是我收到错误:
/bin/su: unrecognized option '--dbname'
如果我将 echo
放在命令的第一位,它将在bash中打印正确的命令,并且如果我复制/粘贴此命令,它将起作用!
If I put echo
in first place of my commands it prints correct command in bash and if I copy/paste this command it works!
请帮助我解决此问题.
推荐答案
您需要提供整个命令以作为单个参数由 su
执行.试试这个:
You need to supply the whole command to execute by su
as a single argument. Try this:
Process createUser = buildProcess(
"/bin/su",
"-c",
"/opt/vmware/vpostgres/current/bin/psql --command " + commandForUserCreation + " --dbname mydbname",
myuser
);
这篇关于Java ProcessBuilder和bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文