試試這個;這不是等待問題。
#!/bin/bash
result=$(bash /home/myscript.sh) # full path of myscript
echo "$result"
還處理如下的bash錯誤;
public static void main(String[] args) throws IOException {
String command = new String("/tmp/1/Test.sh");
Runtime rt = Runtime.getRuntime();
Process process = rt.exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String s;
while ((s = reader.readLine()) != null) {
System.out.println("Script output: " + s);
}
BufferedReader stdError = new BufferedReader(new
InputStreamReader(process.getErrorStream()));
System.out.println("Here is the standard error\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
}
adduser tests;
1- myscript.sh:
#!/bin/bash
adduser test
echo "50"
Java控制檯輸出;
Script output: 50
Here is the standard error of the command (if any):
adduser: Only root may add a user or group to the system.
2- myscript.sh:
#!/bin/bash
sudo adduser test
echo "50"
Java控制檯輸出;
Script output: 50
Here is the standard error of the command (if any):
sudo: no tty present and no askpass program specified
3-
myscript.sh:
#!/bin/bash
echo -e "YOURPASSWORD=\n" | sudo -S adduser -shell /bin/bash --home /home/test test
echo -e "YOURPASSWORD\n" | sudo deluser --remove-home test
echo "OK"
Java控制檯輸出;
Script output: Adding user `test' ...
Script output: Adding new group `test' (1002) ...
Script output: Adding new user `test' (1001) with group `test' ...
Script output: Creating home directory `/home/test' ...
Script output: Copying files from `/etc/skel' ...
Script output: Try again? [y/N] Changing the user information for test
Script output: Enter the new value, or press ENTER for the default
Script output: Full Name []: Room Number []: Work Phone []: Home Phone []: Other []: Is the information correct? [Y/n] Looking for files to backup/remove ...
Script output: Removing files ...
Script output: Removing user `test' ...
Script output: Warning: group `test' has no more members.
Script output: Done.
Script output: OK
Here is the standard error of the command (if any):
[sudo] password for ..: Enter new UNIX password: Retype new UNIX password: passwd: Authentication token manipulation error
passwd: password unchanged
Use of uninitialized value $answer in chop at /usr/sbin/adduser line 563.
Use of uninitialized value $answer in pattern match (m//) at /usr/sbin/adduser line 564.
Use of uninitialized value $answer in chop at /usr/sbin/adduser line 589.
Use of uninitialized value $answer in pattern match (m//) at /usr/sbin/adduser line 590.
檢查錯誤流,因爲它適用於我的代碼 –
試試這個;結果= $(bash /home/myscript.sh),myscript.sh的完整路徑 –