0

Not sure if this is a duplicate question but I really can't find any solution to my problem.

The problem is that I can't run the following command.

#!/bin/bash
SCRIPT1="cd /path/to/project;"
SCRIPT2="git pull;"
ssh "production" ${SCRIPT1} "ssh-agent /bin/bash; ssh-add ~/.ssh/my_bitbucket_key;" ${SCRIPT2}

Still a newbie in bash and ssh so I am not sure also if this is the correct way of doing this.

Hope you can help me. Thanks!

4
  • 2
    Does the command work for somebody else but not for you, or has it never worked for anybody? If it just doesn't work for you, what error message are you getting? If it hasn't worked for anybody, I'd try putting both $SCRIPT1 and $SCRIPT2 inside the double-quotes of the ssh command. Commented Apr 19, 2016 at 1:01
  • change first line to #!/bin/bash -vx to see variable substitutions as they happen in each cmd. I think you'll see the problem. Don't be confused that all strings are now single quoted on the lines beginning with +. That's just the way the debugger "normalizes" its inputs. Good luck. Commented Apr 19, 2016 at 3:09
  • As an aside, putting the remote commands in variables is dubious practice. Perhaps instead create a function which runs its argument in a remote shell, like prod "cd /path/to/project; git pull where prod could be something like prod () { ssh production ssh-agent bash -c "ssh-add ~/.ssh/my_bitbucket_key; $@"; } Commented Apr 19, 2016 at 6:42
  • What does '$@' means? Commented Apr 19, 2016 at 7:22

1 Answer 1

2

The problem with your script is that you are starting ssh-agent running a new instance of bash. The Bash executing your commands has no access to this agent. Instead, you want

ssh "production" "${SCRIPT1}
    eval \$(ssh-agent)
    ssh-add ~/.ssh/my_bitbucket_key
    ${SCRIPT2}"

(The dollar sign after eval has to be escaped because the unescaped dollar signs will be evaluated by the local shell before starting the ssh command.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.