从java运行bash脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9791251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:41:31  来源:igfitidea点击:

Running bash script from java

javabash

提问by dawe134

my problem is simple, when I try to run .sh script from Java, the script doesnt execute. If I change my script to a simple linux command, such as ls -all, it works perfectly, so I guess that I am using a bad command in my script, which stops the execution. Please help.

我的问题很简单,当我尝试从 Java 运行 .sh 脚本时,该脚本不会执行。如果我将我的脚本更改为一个简单的 linux 命令,例如 ls -all,它可以完美运行,所以我猜我在我的脚本中使用了一个错误的命令,这会停止执行。请帮忙。

David

大卫

Java code:

爪哇代码:

    String cmd = "bash /home/david/burza/getter.sh";

    try {
        Process proc = Runtime.getRuntime().exec(new String[] {"/bin/sh", "-c", cmd});
        BufferedReader read = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        try {
            proc.waitFor();
        } catch (InterruptedException e) {
            System.out.println(e.getMessage());
        }
        while (read.ready()) {
            System.out.println(read.readLine());
        }
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }

Bash script:

bash脚本:

#! /bin/bash 

wget -O data1.html http://www.rmsystem.cz/kurzy-online/akcie/easyclick;
touch ext_data.txt; 

grep 'table class="tbl1"' ./data1.html | tr '<td>' ' ' | tr '-' 'A' | grep -o -w '[0-9, ]*' | sed 's/  *//g' | sed '/^$/d' | tr ',' '.' > ext_data.txt;

lines=`wc -l ext_data.txt | grep -o '[0-9]*'`;

( echo $lines; cat ext_data.txt ) > ext_data.txt.new && mv ext_data.txt.new ext_data.txt;

回答by dty

Start by removing the 'bash' from the start of the command.

首先从命令的开头删除“bash”。

Second, I'm not sure the shell will do #! interpretation when not used interactively. I would be minded to jump straight to /bin/bash for the Runtime.exec() call.

其次,我不确定 shell 会不会 #! 不交互使用时的解释。对于 Runtime.exec() 调用,我想直接跳到 /bin/bash 。

Finally, simply having a reader present on the stdout is not going to be enough. You need to actively be reading from it, and from stderr, to prevent the process from hanging if it writes too much output. To clarify, you're waiting for the process to complete (proc.waitFor()) before dealing with any of the output. If the process writes too much output before exiting, it will block waiting for you to empty the buffer while you're blocked waiting for it to exit.

最后,仅仅在标准输出上有一个阅读器是不够的。您需要积极地从它和 stderr 中读取,以防止进程在写入过多输出时挂起。澄清一下,proc.waitFor()在处理任何输出之前,您正在等待过程完成 ( )。如果进程在退出前写了太多输出,它会阻塞等待你清空缓冲区,而你被阻塞等待它退出。

Read all 4 pages of this article: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1

阅读本文的所有 4 页:http: //www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page =1