1

I have the following bash script to start the ssh-agent, add my keys and do a git pull on a repo. The agent is started, the key is added but the git pull doesn't execute...at least nothing is echo'd to the terminal. The git command works if I type it into the terminal...is there something I need to do within my bash script to make it work?

#!/bin/bash

# if we can't find an agent, start one, and restart the script.
if [ -z "$SSH_AUTH_SOCK" ] ; then
  exec ssh-agent bash -c "ssh-add ; $0"
  exit
fi

exec ssh-add ~/.ssh/mykey
git --git-dir=/var/www/node/myapp/.git pull origin master

When I add -x to #!/bin/bash this is the terminal output

+ '[' -z '' ']'
+ exec ssh-agent bash -c 'ssh-add ; ./startgit.sh'
+ '[' -z /tmp/ssh-<redacted>/agent.1733 ']'
+ exec ssh-add /home/ec2-user/.ssh/mykey
Identity added: /home/ec2-user/.ssh/mykey (/home/ec2-user/.ssh/mykey)
1
  • 2
    Are you sure you're restarting the script after the exit if SSH_AUTH_SOCK is empty? Did you try "debugging" it (#!/bin/bash -x to echo the commands being run)? Commented Sep 7, 2014 at 20:14

1 Answer 1

8

The exec command replaces the current shell with the process that is executed. Therefore, the lines that follow it in your script are not executed.

From the manpage:

exec: exec [-cl] [-a name] file [redirection ...]

Exec FILE, replacing this shell with the specified program. You should remove it from the line

exec ssh-add ~/.ssh/mykey

and simply have

ssh-add ~/.ssh/mykey
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.