运行 远程机器上 bash 脚本的一部分

Running part of bash script on a remote machine

我需要 运行 在本地执行一些命令,然后在远程计算机上执行一些命令,所有这些都使用一个本地 bash 脚本。

为简单起见,我想执行以下操作并在我的本地桌面计算机上执行它。

#!/bin/bash

#upload some files to a remote machine
cd /tmp
./upload-files.sh

#run commands on remote machine
ssh myuser:mypass@somewhere.com
cd /tmp/uploads    <--- these commands don't run in the ssh connection
./process-uploads.sh
exit

#run command locally again.
cd -
echo 'complete!'

关于如何做到这一点有什么想法吗?

您可以将 here-docssh 命令一起使用:

#!/bin/bash

#upload some files to a remote machine
cd /tmp
./upload-files.sh

#run commands on remote machine
ssh -t -t myuser:mypass@somewhere.com<<EOF
cd /tmp/uploads
./process-uploads.sh
exit
EOF

#run command locally again.
cd -
echo 'complete!'

如果你想运行只有一个命令:

ssh myuser:mypass@somewhere.com 'cd /tmp/uploads; ./process-uploads.sh'