2

当我想通过 ssh 远程创建目录时,我遇到了 scala 问题。

通过 scala 的 ssh 命令(例如 date 或 ls)可以正常工作。

但是,当我运行例如

"ssh user@Main.local 'mkdir Desktop/test'".!

我得到:bash: mkdir Desktop/test: No such file or directory res7: Int = 127

当我将命令复制粘贴到我的 shell 中时,它会毫无问题地执行。

有人知道发生了什么吗??

编辑:

我找到了这篇文章:sbt (Scala) via SSH results in command not found,但如果我自己做就可以了

但是,我唯一能从中得到的就是使用要创建的目录的完整路径。但是,它仍然不起作用:(

谢谢!

4

1 回答 1

3

ssh doesn't require that you pass the entire command line you want to run as a single argument. You're allowed to pass it multiple arguments, one for the command you want to run, and more for any arguments you want to pass that command.

So, this should work just fine, without the single quotes:

"ssh user@Main.local mkdir Desktop/test"

This shows how to get the same error message in an ordinary bash shell, without involving ssh or Scala:

bash-3.2$ ls -d Desktop
Desktop
bash-3.2$ 'mkdir Desktop/test'
bash: mkdir Desktop/test: No such file or directory
bash-3.2$ mkdir Desktop/test
bash-3.2$ 

For your amusement, note also:

bash-3.2$ mkdir 'mkdir Desktop'
bash-3.2$ echo echo foo > 'mkdir Desktop'/test
bash-3.2$ chmod +x 'mkdir Desktop'/test
bash-3.2$ 'mkdir Desktop/test'
foo

UPDATE:

Note that both of these work too:

Process(Seq("ssh", "user@Main.local", "mkdir Desktop/test")).!
Process(Seq("ssh", "user@Main.local", "mkdir", "Desktop/test")).!

Using the form of Process.apply that takes a Seq removes one level of ambiguity about where the boundaries between the arguments lie. But note that once the command reaches the remote host, it will be processed by the remote shell which will make its own decision about where to put the argument breaks. So for example if you wanted to make a directory with a space in the name, this works locally:

Process(Seq("mkdir", "foo bar")).!

but if you try the same thing remotely:

Process(Seq("ssh", "user@Main.local", "mkdir", "foo bar")).!

You'll get two directories named foo and bar, since the remote shell inserts an argument break.

于 2013-11-13T14:50:33.770 回答