简体   繁体   English

如何使用Java将输入值传递给Shell脚本

[英]How to pass the input value to shell script using java

I have created the shell script to add two numbers. 我创建了shell脚本以添加两个数字。 I want to execute that shell script from java and I am using Jsch library. 我想从Java执行该Shell脚本,并且正在使用Jsch库。 Can you please help me on this. 你能帮我这个忙吗?

first_num=0
second_num=0
echo -n "Enter the first number-->"
read first_num
echo -n "Enter the second number-->"
read second_num
echo "first number + second number = $ (( first_num + second_num ))"

Yes. 是。 I have checked examples in "Jsch" sites, But i am not able to enter the input to shell script. 我已经在“ Jsch”站点中检查了示例,但是我无法将输入输入到Shell脚本。

I tried as you said. 我按照你说的尝试了 Please see the following piece of code 请看下面的代码

String command = "bash /home/opt/addtwonumber.sh 1 2"

And I changed the shell script mentioned below 我更改了下面提到的shell脚本

$first_num
$second_num
echo "first number + second number = $ (( first_num + second_num ))"

But it doesn't work. 但这是行不通的。 i am getting the results like 我得到的结果像

first_number + second_number  = 0

from the examples : 从例子:

    String user = "USERNAME";
    host = "HOSTNAME";

    UserInfo ui=new MyUserInfo();
    session.setUserInfo(ui);
    session.connect();

    Session session=jsch.getSession(user, host, 22);
    String command = "YOUR_SCRIPTFILE_HERE PARAMETER_ONE PARAMETER_TWO";
    Channel channel = session.openChannel("exec");
    ((ChannelExec) channel).setCommand(command);

    channel.setInputStream(null);

    ((ChannelExec) channel).setErrStream(System.err);

    InputStream in = channel.getInputStream();

    channel.connect();

    byte[] tmp = new byte[1024];
    while (true) {
        while (in.available() > 0) {
            int i = in.read(tmp, 0, 1024);
            if (i < 0)
                break;
            System.out.print(new String(tmp, 0, i));
        }
        if (channel.isClosed()) {
            if (in.available() > 0)
                continue;
            System.out.println("exit-status: " + channel.getExitStatus());
            break;
        }
        try {
            Thread.sleep(1000);
        } catch (Exception ee) {
        }
    }
    channel.disconnect();
    session.disconnect();

Mind you : never use while(true) in any production-ready code, thats just ... bad code and it will break / generate deadlocks at some point, no matter how many exceptions you catch, no matter how often you insert statements like break - it will happen. 提醒您: 切勿在任何可用于生产的代码中使用while(true) ,多数民众赞成...错误的代码,无论您捕获了多少异常,无论您插入诸如break -它将发生。

So all you need to do is place your parameters immediately after your scriptfile and that should be about it - but that only works with commandline parameters, AFAIK it isnt easily possible to input parameters during script runtime - read will not work for you as your script apparently isnt local. 因此,您需要做的就是将参数立即放在脚本文件之后,并且应该就此放置它-但仅适用于命令行参数,AFAIK在脚本运行时不容易输入参数- read对您不起作用显然不是本地的。 You cant use scripts which require local input, you need to switch to commandline parameters - namely $1 , $2 and so on. 您不能使用需要本地输入的脚本,而需要切换到命令行参数-即$1$2等。

Heres the interface for MyUserInfo : *click 这是MyUserInfo的界面: *单击

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM