-if [ -f "${SSH_ENV}" ]; then
- . "${SSH_ENV}" > /dev/null
- if ps ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null; then
- # Agent already running, but it may be running without identities:
- if ssh-add -L 2> /dev/null | grep -q 'no identities'; then
- # .. in which case we add them:
- ssh-add
- fi
- else
- start_agent;
- fi
-else
- start_agent;
+MESSAGE=`ssh-add -L 2>&1`
+if [ "$MESSAGE" = 'Could not open a connection to your authentication agent.' -o \
+ "$MESSAGE" = 'Error connecting to agent: Connection refused' -o \
+ "$MESSAGE" = 'Error connecting to agent: No such file or directory' ]; then
+ start_agent
+elif [ "$MESSAGE" = "The agent has no identities." ]; then
+ ssh-add