【问题标题】:Java with shell Script带有 shell 脚本的 Java
【发布时间】:2016-08-04 14:02:40
【问题描述】:

我正在编写一个 Java 程序,从中执行一个 shell 脚本。 shell 脚本正在调用另一个 shell 脚本。我正在尝试将子脚本返回的输出打印到父 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() 但仍然没有用。有人可以帮忙吗?

【问题讨论】:

  • 检查错误流,因为它适用于我的代码
  • 试试这个; result=$( bash /home/myscript.sh ) , myscript.sh 的完整路径

标签: java linux bash shell


【解决方案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);
        }

    }

添加用户测试;

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 错误处理。将捕获什么样的错误?假设我在脚本中使用 "adduser" 命令,如果不是 root 用户,则会抛出 "only root users will be able to add new users" 错误。会在这里拍到吗?
  • 是的,这将被捕获。我也尝试像上面那样进行一些测试;也许这会有所帮助
猜你喜欢
  • 2010-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-06
  • 2017-06-29
相关资源
最近更新 更多