简体   繁体   中英

How to execute git commands from bash script?

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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