Java调用shell脚本并获得结果

Java代码
  1. /** 
  2.      * 运行shell脚本 
  3.      * @param shell 需要运行的shell脚本 
  4.      */  
  5.     public static void execShell(String shell){  
  6.         try {  
  7.             Runtime rt = Runtime.getRuntime();  
  8.             rt.exec(shell);  
  9.         } catch (Exception e) {  
  10.             e.printStackTrace();  
  11.         }  
  12.     }  
  13.   
  14.   
  15. /** 
  16.      * 运行shell 
  17.      *  
  18.      * @param shStr 
  19.      *            需要执行的shell 
  20.      * @return 
  21.      * @throws IOException 
  22.      */  
  23.     public static List runShell(String shStr) throws Exception {  
  24.         List<String> strList = new ArrayList();  
  25.   
  26.         Process process;  
  27.         process = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c",shStr},null,null);  
  28.         InputStreamReader ir = new InputStreamReader(process  
  29.                 .getInputStream());  
  30.         LineNumberReader input = new LineNumberReader(ir);  
  31.         String line;  
  32.         process.waitFor();  
  33.         while ((line = input.readLine()) != null){  
  34.             strList.add(line);  
  35.         }  
  36.           
  37.         return strList;  
  38.     }  
/**
	 * 运行shell脚本
	 * @param shell 需要运行的shell脚本
	 */
	public static void execShell(String shell){
		try {
			Runtime rt = Runtime.getRuntime();
			rt.exec(shell);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


/**
	 * 运行shell
	 * 
	 * @param shStr
	 *            需要执行的shell
	 * @return
	 * @throws IOException
	 */
	public static List runShell(String shStr) throws Exception {
		List<String> strList = new ArrayList();

		Process process;
		process = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c",shStr},null,null);
		InputStreamReader ir = new InputStreamReader(process
				.getInputStream());
		LineNumberReader input = new LineNumberReader(ir);
		String line;
		process.waitFor();
		while ((line = input.readLine()) != null){
		    strList.add(line);
		}
		
		return strList;
	}

 

注:如果sh中含有awk,一定要按new String[]{"/bin/sh","-c",shStr}写,才可以获得流


Java中,你可以使用`ProcessBuilder`类来调用Shell脚本,并获取其执行结果。下面是一个示例代码: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class ShellScriptExample { public static void main(String[] args) { try { // 创建ProcessBuilder对象,并指定要执行的Shell命令 ProcessBuilder processBuilder = new ProcessBuilder("/bin/bash", "-c", "your_shell_script.sh"); // 设置工作目录(可选) processBuilder.directory(new File("your_directory_path")); // 启动进程 Process process = processBuilder.start(); // 读取Shell脚本的输出结果 BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } // 等待脚本执行完成 int exitCode = process.waitFor(); // 输出脚本的退出码 System.out.println("Exit Code: " + exitCode); } catch (IOException | InterruptedException e) { e.printStackTrace(); } } } ``` 在上面的示例中,我们使用`ProcessBuilder`来创建一个进程,并指定要执行的Shell命令。你需要将`your_shell_script.sh`替换为你要执行的实际Shell脚本的路径或命令。如果需要,你还可以通过`processBuilder.directory()`方法设置工作目录。 然后,我们通过`process.getInputStream()`来获取Shell脚本的输出结果,并使用`BufferedReader`来逐行读取输出内容。 最后,我们使用`process.waitFor()`方法等待脚本执行完成,并获取脚本的退出码。你可以根据需要对退出码进行处理。 请注意,这只是一个简单的示例,你可以根据实际需求进行修改和扩展。另外,确保你的Java程序具有执行Shell脚本的权限,并且指定的Shell脚本是可执行的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值