Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
执行shell脚本,其中包含来自Java的while循环_Java_Shell_Process - Fatal编程技术网

执行shell脚本,其中包含来自Java的while循环

执行shell脚本,其中包含来自Java的while循环,java,shell,process,Java,Shell,Process,我在执行包含while循环的shell脚本时遇到了严重的问题。以下是我的shell脚本: echo Here 1 sleep 0.05 echo Here 2 sleep 0.05 echo Here 3 ic=70 while [ $ic -ge 40 ] do #sleep 0.05 ic=$[$ic-1] echo Here $ic done 当我以/home/pi/tbe/testSleep.sh的形式从终端正常执行脚本时,它正在工作。

我在执行包含while循环的shell脚本时遇到了严重的问题。以下是我的shell脚本:

echo Here 1
sleep 0.05
echo Here 2
sleep 0.05
echo Here 3

ic=70
while [ $ic -ge 40 ]
do
        #sleep 0.05
        ic=$[$ic-1]
        echo Here $ic
done
当我以
/home/pi/tbe/testSleep.sh
的形式从终端正常执行脚本时,它正在工作。并打印所有的
echo
s

现在我编写了这个java方法来执行文件:

public static void main(String[] args) throws IOException, InterruptedException {       
    String command = "/home/pi/tbe/testSleep.sh";
    System.out.println("Executing command: " + command);

    Process process = new ProcessBuilder(command).start();

    BufferedReader reader = new BufferedReader(new InputStreamReader(
            process.getInputStream()));
    String line = null;

    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }

    process.waitFor();
    int exitValue = process.exitValue();
    System.out.println("Command executed. Exit value: " + exitValue);
    process.destroy();  
}
当我执行它时,我只能看到以下输出:

Executing command: /home/pi/tbe/testSleep.sh
Here 1
Here 2
Here 3
Here $[70-1]
Command executed. Exit value: 0

这真的很奇怪。任何指针都会对我很有帮助。

我在Ubuntu中运行了你的脚本:

$ sh script.sh
Here 1
Here 2
Here 3
Here $[70-1]
script.sh: 8: [: Illegal number: $[70-1]
显然,$[70-1]没有被计算,只是被当作一个文字来处理


我保存后看到了评论。他们是正确的。使用bash(我很懒)可以得到正确的结果。

执行命令的shell似乎与在命令行上运行脚本的shell不同。将shell指定为bash

在脚本中添加shebang(作为第一行)。在脚本中详细说明shell始终是一个很好的实践

#!/bin/bash
您还可以指定要用于shell脚本的shell

String[] command = {"/bin/bash", "/home/pi/tbe/testSleep.sh"};

指定要用于shell脚本的shell。String[]command={/bin/bash',“/home/pi/tbe/testSleep.sh”};或者在脚本本身中添加shebang-#/bin/bash(作为第一行)。您正在使用
bash
测试脚本。从java进程调用时,使用
sh
调用脚本。脚本使用了
sh
中不支持的功能,因此在行为上有所不同。脚本似乎没有正确解释,因为它不是由shell运行的。您是否尝试添加#/bin/bash?谢谢大家。问题是舍邦。我没有添加#/通过bin/bash,我按照@amit\g的建议创建了我的流程生成器。如果你能把你的评论作为回答,我会接受的。再次感谢。