how to run a bash program from windows in unix usi

2019-08-14 03:49发布

I am trying to run a program which process a file from window using jcraft library in Unix. what i found after establishing the channel it always try to run the program in home directory but i need to run in a separate directory.please have a look what i tried so far and let me know what i am missing.

String strRemoteDir = "/home/process/input" channel = session.openChannel("sftp");

    channel.connect();
    System.out.println("sftp channel opened and connected.");
    channelSftp = (ChannelSftp) channel;
    // Printing Home Directory in Unix Server
    System.out.println(channelSftp.getHome());
    channelSftp.cd(strRemoteDir);
   System.out.println(channelSftp.pwd());

  // for uploading a file where i need to run the program
    File f = new File(fileName);
    channelSftp.put(new FileInputStream(f), f.getName());
    System.out.println("File transfered successfully to host.");
    fileTransfer = true;

    channel=session.openChannel("exec");
    InputStream in=channel.getInputStream();
    // it is printing the desired directory where i want to go
    System.out.println(channelSftp.pwd());
    ((ChannelExec)channel).setCommand("sh process.ksh "a.txt");
    channel.setInputStream(null);
    ((ChannelExec)channel).setErrStream(System.err);
     channel.connect();

output : process.ksh not found

But through putty i was able to run the program. just to let you know process.ksh is not in the input directory, but has the capability to run from anywhere with arguments. ((ChannelExec)channel).setcommand("ls") prints out all the files from home directory. i believe i am establishing a channel to home directory, i just don't know how to run bash program using jcraft in a desired location.Please let me know what i am missing or is it possible to make it happen.

Thanks in advance. Nur

2条回答
Emotional °昔
2楼-- · 2019-08-14 04:00

"sftp" channel are not made to execute shell command but sftp command only.

    Channel channel = session.openChannel("shell");
    cmdSend = channel.getOutputStream();
    InputStream cmdRcv = channel.getInputStream();
    // Start a Thread reading and displaying cmdRcv

    channel.connect(3000);
    Thread.sleep(1000);

    cmdSend.write("cd /to/the/right/dir\n".getBytes());
    cmdSend.flush();
    cmdSend.write("sh process.ksh \"a.txt\"\n".getBytes());
    cmdSend.flush();
查看更多
ゆ 、 Hurt°
3楼-- · 2019-08-14 04:12

Others have provided examples of how to set the working directory. However, a more defensive style of programming is to assume nothing and specify everything explicitly. So for specifying the command to execute, something like:

/bin/sh /path/to/bin/process.ksh /path/to/data/ "a.txt"

Notice that I have added a new first argument to your command, the directory you want to run it from.

Now, change your script so that it changes to this directory (given as parameter $1) before continuing.

This can be done by adding cd at the beginning, something like:

cd $1
shift

The shift command shifts all other arguments, so that $2 becomes $1 and so on, so that the rest of the script will find the arguments where it expects.

查看更多
登录 后发表回答