简体   繁体   English

ssh服务器bash -c“ cd / tmp && git pull”,cd不起作用,需要先添加echo

[英]ssh server bash -c “cd /tmp && git pull” , cd does not work, need to add echo first

I'm on ubuntu 15.04, my version of ssh client is 我在ubuntu 15.04上,我的ssh客户端版本是

OpenSSH_6.9p1 Ubuntu-2ubuntu0.2, OpenSSL 1.0.2d 9 Jul 2015

When I try to run the following command ssh admin@server bash -c 'cd /path/to/repo && git pull' the cd is not effective and i got 当我尝试运行以下命令ssh admin@server bash -c 'cd /path/to/repo && git pull' ,cd无效,我得到了

fatal: Not a git repository (or any of the parent directories): .git

However If I do 但是如果我这样做

ssh admin@server bash -c 'echo test && cd /path/to/repo && git pull'

then it works 然后它起作用

Already up-to-date.

Of course I'm well aware echo is not supposed to change anything but after trying several time, several days on several different servers (though all on debian) I'm now sure to have this error. 当然,我很清楚echo不应该改变任何东西,但是在几台不同的服务器上尝试了好几天又几天后(尽管全部在debian上),我现在肯定会出现此错误。 On other servers I tried the command cd /tmp && pwd , and I got my home directory, and if i do echo toto && /tmp && pwd I go /tmp printed... 在其他服务器上,我尝试了命令cd /tmp && pwd ,我得到了主目录,如果我对echo toto && /tmp && pwd做了echo toto && /tmp && pwd我会/tmp打印...

Unfortunately, ssh passes through a single command line string to $SHELL -c on the remote. 不幸的是,ssh通过单个命令行字符串传递给远程服务器上的$SHELL -c Your quotes aren't being effective. 您的报价无效。

When you run 当你跑步

ssh admin@server bash -c 'cd /path/to/repo && git pull'

this is being run on the remote server (with $SHELL -c ): 这正在远程服务器上运行(使用$SHELL -c ):

bash -c cd /path/to/repo && git pull

So Bash is given single command ( cd ) and an unused argument, and then separately, you're also running git pull in the home directory. 因此,为Bash提供了单个命令( cd )和一个未使用的参数,然后分别为您在主目录中运行git pull

On the other hand, when you run 另一方面,当您跑步时

ssh admin@server bash -c 'echo test && cd /path/to/repo && git pull'

this is being run on the remote server: 这是在远程服务器上运行:

bash -c echo test && cd /path/to/repo && git pull

The first part is again useless, but the shell running the whole command then does cd /path/to/repo and git pull . 第一部分同样没有用,但是运行整个命令的shell然后执行cd /path/to/repogit pull Which works. 哪个有效。

What you probably want to do is 您可能想做的是

ssh admin@server 'cd /path/to/repo && git pull'

The existing answer by ephemient is entirely correct in terms of cause. 暂时性现有答案在原因上是完全正确的。

To add an alternate solution -- one which works when your remote code contains constructs which sh -c will misinterpret -- consider: 要添加另一种解决方案-当您的远程代码包含sh -c会误解的构造时可以使用的解决方案-考虑:

repo=/path/to/repo                 ## here, this works even when your path contains
                                   ## nonprintable or otherwise surprising characters
printf -v repo_q '%q' "$repo"      ## ...because we're asking your local copy of bash
                                   ## to generate a quoted/escaped copy of the value
                                   ## that will 'eval' back to its original meaning
                                   ## when interpreted by bash

## to ensure that it's interpreted by bash, we pass 'bash -s' as the command to ssh
## with an *unquoted* heredoc (<<EOF, vs <<'EOF'), with the escaped value expanded
ssh admin@server 'bash -s' <<EOF
cd $repo_q && git pull
EOF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM