Table of Contents

概要

ソース

#!/bin/bash
# https://superuser.com/a/950479

agent_out_file="$HOME/ssh-agent.out"

function initialize {
  pkill ssh-agent
  ssh-agent > $agent_out_file 
  . $agent_out_file
}

pgrep ssh-agent
if [ $? -eq 0 ]; then # ssh agent running
  ssh-add -l > /dev/null 2>&1
  status=$?
  if [ $status -eq 0 ]; then # can connect to ssh agent and keys available
    echo nothing to do
  elif [ $status -eq 1 ]; then # can connect to ssh agent and no keys available
    ssh-add ~/.ssh/id_rsa
  elif [ $status -eq 2 ]; then # cannot connect to ssh agent
    . $agent_out_file
  fi
else # ssh agent not running
  initialize
fi