1

I have got this requirement where I need to execute a list of unix commands and then against each command I should store its result. Eg. Commands - cd dir1, pwd, ls

My output should contain -

cd dir1- pwd-dir1 ls-a, b, c, d(assume these are present under dir1 directory)

I tried using JSch library and tried using shell and exec channel types.

But this does not allow me to send a command and read output and then send another and read output.

What I did as of now is Using exec send all the commands and an echo command after each command and then read till that echo for that command output.

Using shell I sent commands but it waits for an exit command other wise it goes to infinite wait.

Also how can we get the error for individual commands

Please suggest if there is any way to achieve this.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992

1 Answers1

0

If the commands are independent, open a new "exec" channel for each.


If the later commands depend on previous commands, there's no nice solution.

You have actually already found the best way:

What I did as of now is Using exec send all the commands and an echo command after each command and then read till that echo for that command output.


See also JSch Shell channel execute commands one by one testing result before proceeding.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992