sudo
我有这段代码可以使用和不使用选项来执行命令
String sudoScript = "sudo -u root -S ls /";
String script = "ls /";
try {
System.out.println("===================================");
System.out.println("command="+sudoScript);
Process p1 = Runtime.getRuntime().exec(sudoScript);
System.out.println("p2.waitFor()="+p1.waitFor());
BufferedReader stdInput1 = new BufferedReader(new InputStreamReader(p1.getInputStream()));
while ((sudoScript = stdInput1.readLine()) != null) {
System.out.println(script);
}
System.out.println("===================================");
System.out.println("command="+script);
Process p2 = Runtime.getRuntime().exec(script);
System.out.println("p2.waitFor()="+p2.waitFor());
BufferedReader stdInput2 = new BufferedReader(new InputStreamReader(p2.getInputStream()));
while ((script = stdInput2.readLine()) != null) {
System.out.println(script);
}
} catch (Exception e) {
e.printStackTrace();
}
这就是我得到的那种输出-
===================================
command=sudo -u root -S ls /
p2.waitFor()=1
===================================
command=ls /
p2.waitFor()=0
bin
boot
cgroup
data
dev
etc
home
home.save
lib
lost+found
media
mnt
null
opt
opt.save
proc
root
sbin
selinux
srv
sys
tmp
usr
var
如果您观察到,我无法使用sudo
命令获得相同的输出。我在这里错过了什么吗?