我在 ubuntu 15.04 上,我的 ssh 客户端版本是
OpenSSH_6.9p1 Ubuntu-2ubuntu0.2, OpenSSL 1.0.2d 9 Jul 2015
当我尝试运行以下命令时 ssh admin@server bash -c 'cd /path/to/repo && git pull' ,cd 无效,我得到了
fatal: Not a git repository (or any of the parent directories): .git
Run Code Online (Sandbox Code Playgroud)
但是如果我这样做
ssh admin@server bash -c 'echo test && cd /path/to/repo && git pull'
然后它起作用了
Already up-to-date.
Run Code Online (Sandbox Code Playgroud)
当然,我很清楚echo不应该改变任何东西,但是在几个不同的服务器上尝试了几次之后几天(虽然都是在 debian 上)我现在肯定会遇到这个错误。在其他服务器上,我尝试了命令cd /tmp && pwd,然后得到了我的主目录,如果我这样做了,echo toto && /tmp && pwd我会/tmp打印...
不幸的是,ssh 通过单个命令行字符串传递$SHELL -c到远程。您的报价无效。
当你跑
ssh admin@server bash -c 'cd /path/to/repo && git pull'
Run Code Online (Sandbox Code Playgroud)
这是在远程服务器上运行的(使用$SHELL -c):
bash -c cd /path/to/repo && git pull
Run Code Online (Sandbox Code Playgroud)
所以 Bash 被赋予单个命令 ( cd) 和一个未使用的参数,然后单独地,您也在git pull主目录中运行。
另一方面,当你跑步时
ssh admin@server bash -c 'echo test && cd /path/to/repo && git pull'
Run Code Online (Sandbox Code Playgroud)
这是在远程服务器上运行的:
bash -c echo test && cd /path/to/repo && git pull
Run Code Online (Sandbox Code Playgroud)
第一部分再次没用,但是运行整个命令的 shell 然后执行cd /path/to/repoand git pull。哪个有效。
你可能想要做的是
ssh admin@server 'cd /path/to/repo && git pull'
Run Code Online (Sandbox Code Playgroud)