@@ -25,7 +25,19 @@ TMPDIR=${1-/tmp}
2525# See README file, SSH-AGENT SOCKET note
2626_sockfile=" ${HOME} /.ssh/agent.sock"
2727
28- if [ " $0 " != " -bash" ] && [ " $0 " != " -zsh" ];
28+ is_login_shell_bash ()
29+ {
30+ expr " $SHELL " : ' .*bash' >> /dev/null
31+ if [ $? -eq 0 ]; then
32+ # echo "yes"
33+ return 0
34+ else
35+ # echo "no"
36+ return 1
37+ fi
38+ }
39+
40+ if is_login_shell_bash && [ " $0 " != " -bash" ];
2941then
3042 echo '
3143OOPS! Did you mean to source this script? Try this:
@@ -34,9 +46,9 @@ OOPS! Did you mean to source this script? Try this:
3446'
3547fi
3648
37- if [ ! -z " $_sockfile " ];
49+ if [ -n " $_sockfile " ];
3850then
39- _agent_opts=" -a $_sockfile $_agent_opts "
51+ _agent_opts=" -s - a $_sockfile $_agent_opts "
4052fi
4153
4254main ()
6678 then
6779 echo " ssh-agent socket is stale"
6880 kill " ${_agent_pid} " # kill ssh-agent
81+ killall pgrep ssh-agent -U " $USER "
6982 cleanup_stale_agent
7083 start_ssh_agent
7184 return
@@ -74,18 +87,16 @@ main()
7487
7588set_agent_pid ()
7689{
77- # echo "checking for ssh-agent process"
78- _agent_pid=$( pgrep -U " $USER " ssh-agent)
79- # this is expensive but reliable
80- # _agent_pid=$(ps uwwxU "$USER" | grep ssh-agent | grep -v grep | awk '{ print $2 }' | tail -n1)
90+ # echo "checking for ssh-agent process"
91+ _agent_pid=$( pgrep -U " $USER " ssh-agent | tail -n1)
8192}
8293
8394set_socket_file ()
8495{
8596 # if _sockfile is not defined we must figure it out
8697 if [ -z " $_sockfile " ] || [ ! -e " $_sockfile " ];
8798 then
88- _sock_pid=$( echo " ${_agent_pid} - 1" | bc)
99+ _sock_pid=$( echo " ${_agent_pid} " - 1 | bc)
89100 _sockfile=$( /bin/ls " $TMPDIR /ssh-*/agent.${_sock_pid} " )
90101 fi
91102
@@ -122,14 +133,19 @@ discover_ssh_agent()
122133start_ssh_agent ()
123134{
124135 echo " starting ssh-agent $_agent_opts "
125- ssh-agent $_agent_opts > /dev/null
136+ # shellcheck disable=SC2046,SC2086
137+ eval $( ssh-agent $_agent_opts ) || return
126138
127139 discover_ssh_agent
128140
129- if [ ! -z " $SSH_AUTH_SOCK " ];
141+ if [ -n " $SSH_AUTH_SOCK " ];
130142 then
131- # this will prompt the user to authenticate their ssh key(s)
132143 echo " adding ssh key(s) to agent"
144+ if [ " $( uname) " = " Darwin" ]; then
145+ # loads SSH keys stored in OS X Keychain
146+ ssh-add -A
147+ fi
148+ # this will prompt the user to authenticate their password protected ssh key(s)
133149 ssh-add
134150 fi
135151}
0 commit comments