Reputation: 10881
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)
Upvotes: 1
Views: 3605
Reputation: 59072
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
Upvotes: 8