我正在通过 shell 脚本登录远程机器(通过在脚本中放置 ssh 命令)。 在 ssh 命令之后,脚本的其余行将在当前机器而不是远程机器上执行。如何使其余的 shell 脚本行在远程机器上执行。?
假设这是我的脚本
ssh username@ip-address
ls
whoami
----
ssh 之后的其余行应该在远程机器而不是当前机器上执行。如何实现?
最佳答案
一个可能的解决方案是使用 heredoc
,如下例所示:
$ ssh example.foo.com -- <<@@
> ls /etc/
> cat /etc/passwd
> @@
基本上第一行@@和最后一行之间的所有内容都会在远程机器上执行。
您还可以通过将文件内容读入变量来使用文件内容:
$ MYVAR=`cat ~/foo.txt`
$ ssh example.foo.com -- <<@@
> $MYVAR
> @@
或者通过简单地在 heredoc 中执行相同的操作:
$ ssh example.foo.com -- <<@@
> `cat ~/foo.txt`
> @@
关于linux - 在远程机器上执行部分 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19265375/