Java程序调用带参数的shell脚本返回值

简介: Java程序调用带参数的shell脚本返回值首先来看看linux中shell变量($#,$@,$0,$1,$2)的含义解释 变量说明:$$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行的命令的结束代码(返回值) $- 使用Set命令设定的Flag一览 $* 所有参数列表。

Java程序调用带参数的shell脚本返回值

首先来看看linux中shell变量($#,$@,$0,$1,$2)的含义解释
变量说明:

  • $$
    Shell本身的PID(ProcessID)
  • $!
    Shell最后运行的后台Process的PID
  • $?
    最后运行的命令的结束代码(返回值)
  • $-
    使用Set命令设定的Flag一览
  • $*
    所有参数列表。如”$*”用「”」括起来的情况、以”$1 $2 … $n”的形式输出所有参数。
  • $@
    所有参数列表。如”$@”用「”」括起来的情况、以”\$1” “\$2” … “\$n” 的形式输出所有参数。
  • $#
    添加到Shell的参数个数 $0 Shell本身的文件名 $1~$n 添加到Shell的各参数值。$1是第1参数、$2是第2参数…。

Java程序调用带参数的shell脚本返回值实现具体代码

package com.javen.kit;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;

public class ShellKit {
    /**
     * 运行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
     * 注:如果sh中含有awk,一定要按new String[]{"/bin/sh","-c",shStr}写,才可以获得流.
     */
    public static  List<String> runShell(String shStr) throws Exception {
        List<String> strList = new ArrayList<String>();

        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;
    }
}

例子

假设有一个shell脚本文件test.sh,有两个参数parm1,parm2,java调用的方法如下:

String[] cmd = {"/bin/sh","-c","test.sh parm1 parm2"}; 
Runtime.getRuntime().exec(cmd); 

上面的ShellKit.java就是对该方法的封装

test.sh

#!/bin/sh
#Author : Javen

printf "The complete list is %s\\n" "$$"
printf "The complete list is %s\\n" "$!"
printf "The complete list is %s\\n" "$?"
printf "The complete list is %s\\n" "$*"
printf "The complete list is %s\\n" "$@"
printf "The complete list is %s\\n" "$#"
printf "The complete list is %s\\n" "$0"
printf "The complete list is %s\\n" "$1"
printf "The complete list is %s\\n" "$2

服务器测试

[root@iZ94hjirr10Z software]# ./test.sh Javen205 572839485
The complete list is 15409
The complete list is
The complete list is 0
The complete list is Javen205 572839485
The complete list is Javen205
The complete list is 572839485
The complete list is 2
The complete list is ./test.sh
The complete list is Javen205
The complete list is 572839485

程序调用

public class ShellController extends Controller {

    public void index(){
        String shell = getPara("shell");
        ShellKit.execShell(shell);
        renderText("执行完成...");
    }


    public void runShell(){
        String shStr = getPara("shell");
        try {
            List<String> list = ShellKit.runShell(shStr);
            renderJson(list);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }



}

浏览器测试 并返回结果

http://120.xxx:8080/Demo/shell/runShell?shell=/home/software/test.sh      Javen205 572839485

浏览器测试 不返回结果

http://120.xxx:8080/Demo/shell?shell=/home/software/test.sh       Javen205 572839485

返回结果

["The complete list is 15416","The complete list is ","The complete list is 0","The complete list is Javen205 572839485","The complete list is Javen205","The complete list is 572839485","The complete list is 2","The complete list is /home/software/test.sh","The complete list is Javen205","The complete list is 572839485"]

如有疑问欢迎留言

目录
相关文章
|
25天前
|
弹性计算 Shell Perl
ecs服务器shell常用脚本练习(二)
【4月更文挑战第1天】shell代码训练(二)
106 1
|
28天前
|
Java Shell
SpringBoot启动脚本Shell
SpringBoot启动脚本Shell
18 0
|
5天前
|
Java 关系型数据库 MySQL
Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
【4月更文挑战第12天】Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
33 3
|
28天前
|
缓存 Java C#
【JVM故障问题排查心得】「Java技术体系方向」Java虚拟机内存优化之虚拟机参数调优原理介绍(一)
【JVM故障问题排查心得】「Java技术体系方向」Java虚拟机内存优化之虚拟机参数调优原理介绍
79 0
|
2天前
|
监控 Shell 应用服务中间件
第十二章 Shell脚本编写及常见面试题(二)
第十二章 Shell脚本编写及常见面试题(二)
|
2天前
|
监控 关系型数据库 Shell
第十二章 Shell脚本编写及常见面试题(一)
第十二章 Shell脚本编写及常见面试题(一)
|
2天前
|
监控 Shell
生产环境Shell脚本Ping监控主机是否存活(多种方法)
生产环境Shell脚本Ping监控主机是否存活(多种方法)
|
2天前
|
运维 Shell
Shell脚本判断IP是否合法性(多种方法)
Shell脚本判断IP是否合法性(多种方法)
|
2天前
|
存储 Java 测试技术
一文搞清楚Java中的方法、常量、变量、参数
在JVM的运转中,承载的是数据,而数据的一种变现形式就是“量”,量分为:**常量与变量**,我们在数学和物理学中已经接触过变量的概念了,在Java中的变量就是在程序运行过程中可以改变其值的量。
14 0
|
8天前
|
运维 监控 Shell
利用Shell脚本编写局域网监控软件:实时监测主机连接情况
本文介绍了如何使用Shell脚本创建一个局域网监控工具,以实时检查主机连接状态。脚本包括扫描IP地址范围检测主机可达性及使用`netstat`监控ESTABLISHED连接。此外,还展示了如何每60秒将连接数数据自动提交到指定网站API,以便实时跟踪网络活动。这个自动化监控系统有助于提升网络安全性和故障排查效率。
32 0