gpt4 book ai didi

java - 无法从 java 运行 shell 脚本

转载 作者:行者123 更新时间:2023-12-02 07:43:33 25 4
gpt4 key购买 nike

我有一个 shell 脚本,它有一个命令可以在成功创建脚本后生成一个包含所需信息的文件。

现在,当我直接执行该命令或从命令行执行脚本本身时,就会生成文件。

但是当我尝试使用 Runtime.getRuntime().exec(cmd) 从 java 代码执行相同的脚本时,文件不会生成。该行之前和之后的跟踪是按预期显示表明脚本的执行没有抛出任何异常。

我哪里出错了或者我错过了什么?请帮助!!!

示例这是我尝试执行的代码

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Scripttest {
public static void main(String args[])
{
try{
String cmd="ls|grep sys";
Process p=Runtime.getRuntime().exec(cmd);
System.out.println("done");
final BufferedReader input = new BufferedReader(new InputStreamReader( p.getInputStream()));
String line = null;
while ((line = input.readLine()) != null)
System.out.println("proc: " + line);
}
catch(Exception e)
{
System.out.println("Exception is"+e);
}
}
}

我得到的输出是这样的

java Scripttest
Exception isjava.io.IOException: Cannot run program "ls|grep": error=2, No such file or directory

cmd 没有被执行,但不确定错误原因。

最佳答案

这是一个类似于使用管道的示例,在此示例中脚本嵌入在 java 中。

import java.io.*;
class junk {
public static void main (String args[]) {
try {
String line; String script;
OutputStream stdin = null; InputStream stderr = null; InputStream stdout = null;
Process p = Runtime.getRuntime ().exec ("/bin/bash");
stdin = p.getOutputStream (); stderr = p.getErrorStream (); stdout = p.getInputStream ();
script = "a=$(cat <<'@@@'\n" +
"ICAgICBfCiAgICB8IHwgX18gX19fICAgX19fXyBfICAgIF9fXyBfXyBfIF8gX18gICAgXyBfXyBfICAg" +
"XyBfIF9fCiBfICB8IHwvIF9gIFwgXCAvIC8gX2AgfCAgLyBfXy8gX2AgfCAnXyBcICB8ICdfX3wgfCB8" +
"IHwgJ18gXAp8IHxffCB8IChffCB8XCBWIC8gKF98IHwgfCAoX3wgKF98IHwgfCB8IHwgfCB8ICB8IHxf" +
"fCB8IHwgfCB8CiBcX19fLyBcX18sX3wgXF8vIFxfXyxffCAgXF9fX1xfXyxffF98IHxffCB8X3wgICBc" +
"X18sX3xffCB8X3wKICAgICAgICAgICAgICAgICAgICBfICAgICAgICAgICAgICAgXwogICAgICAgICAg" +
"ICAgICAgICAgfCB8X18gICBfXyBfIF9fX3wgfF9fCiAgICAgICAgICAgICAgICAgICB8ICdfIFwgLyBf" +
"YCAvIF9ffCAnXyBcCiAgICAgICAgICAgICAgICAgICB8IHxfKSB8IChffCBcX18gXCB8IHwgfAogICAg" +
"ICAgICAgICAgICAgICAgfF8uX18vIFxfXyxffF9fXy9ffCB8X3wK" +
"\n" +
"@@@)\n" +
"st=0\n" +
"for (( i=0; i<\"${#a}\"; i++ ))\n" +
"do\n" +
" x=${a:$i:1}\n" +
" in=$(($(expr index \\\n" +
" 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' \"\\\\$x\")-1))\n" +
" if [ $in -ge 0 ]; then case $st in\n" +
" 0 ) out=$(($in<<2)); st=3;;\n" +
" 1 ) out=$(($out|$in)); \n" +
" printf \\\\$(printf '%03o' $(($out&255)) ) ; st=0 ;;\n" +
" 2 ) out=$(($out+($in>>2))); \n" +
" printf \\\\$(printf '%03o' $(($out&255)) ) ;\n" +
" st=0; out=$(($in<<6)); st=1;;\n" +
" * ) out=$(($out+($in>>4))); \n" +
" printf \\\\$(printf '%03o' $(($out&255)) ) ;\n" +
" st=0; out=$(($in<<4)); st=2;;\n" +
" esac fi\n" +
"done\n";
stdin.write (script.getBytes ());
stdin.close ();
BufferedReader br = new BufferedReader (new InputStreamReader (stdout));
while ((line = br.readLine ()) != null) { System.out.println(line); }
br.close ();
br = new BufferedReader (new InputStreamReader (stderr));
while ((line = br.readLine ()) != null) { System.out.println ("2>" + line); }
br.close ();
p.waitFor ();
System.out.println ("exit code " + p.exitValue ());
}
catch (IOException e) { e.printStackTrace (); }
catch (java.lang.InterruptedException e) { e.printStackTrace (); }
}
}

关于java - 无法从 java 运行 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11239523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com