2016-08-04 85 views
1

我在寫一個Java程序,我正在執行一個shell腳本。 shell腳本正在調用另一個shell腳本。我正嘗試將子腳本返回的輸出打印到父shell腳本。 以下是我的代碼。Java與shell腳本

public class App 
{ 
    public static void main(String[] args) throws IOException, InterruptedException 
    { 
     String command = new String("/home/Test.sh"); 
     Runtime rt = Runtime.getRuntime(); 
     Process process = rt.exec(command); 
     process.waitFor(1, TimeUnit.MINUTES); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(  
       process.getInputStream()));           
     String s;                 
     while ((s = reader.readLine()) != null) {         
      System.out.println("Script output: " + s);        
     } 

Shell腳本:Test.sh

#!/bin/bash 
    result=$(bash myscript.sh) 
    echo "$result" 

myscript.sh

#!/bin/bash 
    echo "50" 

我得到空的輸出。我最初認爲這可能是因爲Java進程不在等待shell腳本完成。所以添加了waitFor(),但仍然沒有用。有人可以幫忙嗎?

+1

檢查錯誤流,因爲它適用於我的代碼 –

+0

試試這個;結果= $(bash /home/myscript.sh),myscript.sh的完整路徑 –

回答

1

試試這個;這不是等待問題。

#!/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. 
+0

謝謝..添加完整路徑有所幫助。我喜歡理解bash錯誤處理。什麼樣的錯誤將被捕獲?假設我在腳本中使用**「adduser」**命令,**「只有root用戶才能添加新用戶」**如果不是root用戶,將會拋出錯誤。這會被捕獲嗎? – Learner

+1

是的,這將被捕獲。我也嘗試上面的一些測試;也許這可以幫助 –