gpt4 book ai didi

java - 如何用java一步步执行linux shell

转载 作者:太空宇宙 更新时间:2023-11-04 04:55:42 24 4
gpt4 key购买 nike

Linux(centos 6):

第 1 步

$:yum install xxx

返回一些消息,例如

"Are you OK with your IP being logged? (Y)es/(N)o"

第 2 步

$:Y

安装正在进行。(其他命令可能有步骤 3)

但是我怎样才能用java执行这个命令呢?

这是我的java代码:

String[] commands = new String[]{"yum","install","xxx"};
Runtime run = Runtime.getRuntime();
Process pr = run.exec(commands);
Reader reader = new InputStreamReader(pr.getInputStream());
BufferedReader bf = new BufferedReader(reader);

bf 包括

"Are you OK with your IP being logged?"

现在我如何执行命令(“Y”)以便安装可以继续

请帮助我。

最佳答案

我不知道如何使用“y”(是)执行命令,但您可以按如下方式运行 yum

yum -y install xxx

-y, --assumeyes Assume yes; assume that the answer to any question which would be asked is yes. Configuration Option: assumeyes See: https://linux.die.net/man/8/yum

关于java - 如何用java一步步执行linux shell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46686663/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com