1

I attempt to move my shell script to scala code with package of sys.process.
I find it can't save the shell context, that means, I can't replace "ls ./somedir" ! with "cd ./somedir" ! "ls ./" !.
(ps: In this way, notation ! will consume string as shell command and execute it after import sys.process._ in scala)

How to execute shell commands and save it context just like terminal does?

also, hope it could support ssh name@host to open a remote session.

Thanks.

Joe C
  • 15,324
  • 8
  • 38
  • 50
LoranceChen
  • 2,453
  • 2
  • 22
  • 48
  • A process running in the JVM isn't supposed to be able to modify its own environment. Refer to [this question](https://stackoverflow.com/questions/318239/how-do-i-set-environment-variables-from-java), and the answers, for more on this topic. – jwvh Aug 05 '17 at 16:30
  • I just find library called JSch. it support InputStream and OutputStream to deal shell command.But it need a wrapper to use happily and I'm doing this.Besides, because it based on SSH, so open a local SSH if you want invoke local shell command ;-) – LoranceChen Aug 06 '17 at 07:50

2 Answers2

0

Ammonite shell purports to be the bash replacement you're looking for. It supports a notion of working directory, for example, as well as conveniences for running commands.

som-snytt
  • 39,429
  • 2
  • 47
  • 129
  • Hi, Could I use shell command in my program rather then in REPL. – LoranceChen Aug 06 '17 at 06:05
  • I think it makes most sense to replace a bash script with a Scala script, but you can also embed ammonite http://ammonite.io/#Embedding or it's just a couple of libs if you want to reinvent your wheels https://github.com/lihaoyi/ammonite – som-snytt Aug 06 '17 at 21:10
  • Embedding seems has some bug at this point: https://github.com/lihaoyi/Ammonite/issues/276#issuecomment-320557669 – LoranceChen Aug 07 '17 at 03:03
  • They have a gitter channel if you can access it. https://gitter.im/lihaoyi/Ammonite – som-snytt Aug 08 '17 at 00:15
0

Have build a wheel at git repo.
Basic shell command will use happily. advanced usage need improve yet.

For example:
create a ssh shell:
val shell = new Shell(Auth("localhost", "username", 22, "password"))

execute shell command:
val lsRst: Either[Int, String] = shell.exc("ls")
Besides, result Left means fail, Right means success.
You can get Right String data and mix with scala language.

LoranceChen
  • 2,453
  • 2
  • 22
  • 48