Skip to content

Commit 9ce0dfb

Browse files
committed
shellcheck: quote variable strings
1 parent 94a018c commit 9ce0dfb

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

agent.sh

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ TMPDIR=${1-/tmp}
2525
# See README file, SSH-AGENT SOCKET note
2626
_sockfile="${HOME}/.ssh/agent.sock"
2727

28-
if [ "$0" != "-bash" ];
28+
if [ "$0" != "-bash" ] && [ "$0" != "-zsh" ];
2929
then
3030
echo '
3131
OOPS! Did you mean to source this script? Try this:
@@ -43,18 +43,18 @@ main()
4343
{
4444
discover_ssh_agent
4545

46-
# 1. ssh-agent not running.
46+
# 1. ssh-agent not running.
4747
if [ -z "${_agent_pid}" ];
4848
then
4949
cleanup_stale_agent
5050
start_ssh_agent
5151
return
5252
fi
5353

54-
# 2. ssh-agent is running but...
55-
#
56-
# SSH_AGENT_PID is set but different than running ssh-agent pid
57-
if [ ! -z $SSH_AGENT_PID ] && [ $SSH_AGENT_PID -ne "${_agent_pid}" ];
54+
# 2. ssh-agent is running but...
55+
#
56+
# SSH_AGENT_PID is set but different than running ssh-agent pid
57+
if [ ! -z "$SSH_AGENT_PID" ] && [ "$SSH_AGENT_PID" -ne "${_agent_pid}" ];
5858
then
5959
cleanup_stale_agent
6060
discover_ssh_agent
@@ -65,7 +65,7 @@ main()
6565
if [ $? -eq 2 ]; # communication failed
6666
then
6767
echo "ssh-agent socket is stale"
68-
kill ${_agent_pid} # kill ssh-agent
68+
kill "${_agent_pid}" # kill ssh-agent
6969
cleanup_stale_agent
7070
start_ssh_agent
7171
return
@@ -74,18 +74,19 @@ main()
7474

7575
set_agent_pid()
7676
{
77-
# this is expensive but reliable
7877
#echo "checking for ssh-agent process"
79-
_agent_pid=`ps uwwxU $USER | grep ssh-agent | grep -v grep | awk '{ print $2 }' | tail -n1`
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)
8081
}
8182

8283
set_socket_file()
8384
{
8485
# if _sockfile is not defined we must figure it out
8586
if [ -z "$_sockfile" ] || [ ! -e "$_sockfile" ];
8687
then
87-
_sock_pid=`echo "${_agent_pid} - 1" | bc`
88-
_sockfile=`/bin/ls $TMPDIR/ssh-*/agent.${_sock_pid}`
88+
_sock_pid=$(echo "${_agent_pid} - 1" | bc)
89+
_sockfile=$(/bin/ls "$TMPDIR/ssh-*/agent.${_sock_pid}")
8990
fi
9091

9192
if [ ! -e "$_sockfile" ];
@@ -101,19 +102,19 @@ discover_ssh_agent()
101102
{
102103
set_agent_pid
103104

104-
if [ -z $_agent_pid ]; # no agent PID discovered
105+
if [ -z "$_agent_pid" ]; # no agent PID discovered
105106
then
106107
echo "ssh agent is not running"
107108
return
108109
fi
109110

110111
echo "ssh agent for $USER found at pid ${_agent_pid}."
111-
export SSH_AGENT_PID=${_agent_pid}
112+
export SSH_AGENT_PID="${_agent_pid}"
112113

113114
set_socket_file
114115

115116
# make sure the Mac Login environment is configured
116-
if [ "`uname`" == "Darwin" ]; then
117+
if [ "$(uname)" = "Darwin" ]; then
117118
setup_plist
118119
fi
119120
}
@@ -125,7 +126,7 @@ start_ssh_agent()
125126

126127
discover_ssh_agent
127128

128-
if [ ! -z $SSH_AUTH_SOCK ];
129+
if [ ! -z "$SSH_AUTH_SOCK" ];
129130
then
130131
# this will prompt the user to authenticate their ssh key(s)
131132
echo "adding ssh key(s) to agent"
@@ -137,15 +138,15 @@ setup_plist()
137138
{
138139
_envdir="$HOME/.MacOSX"
139140

140-
if [ ! -d $_envdir ]; then
141-
mkdir $_envdir
141+
if [ ! -d "$_envdir" ]; then
142+
mkdir "$_envdir"
142143
fi
143144

144145
_plist="$_envdir/environment.plist"
145146

146-
if [ -e $_plist ];
147+
if [ -e "$_plist" ];
147148
then
148-
grep -q $_sockfile $_plist
149+
grep -q "$_sockfile" "$_plist"
149150
ENVSET=$?
150151
if [ ! $ENVSET ]; then
151152
set +o noclobber
@@ -171,29 +172,29 @@ write_plist()
171172
</dict>
172173
</plist>
173174
EOXML
174-
) > $_plist
175+
) > "$_plist"
175176
}
176177

177178
cleanup_stale_agent()
178179
{
179180
# check the environment variable SSH_AGENT_PID as it could be set
180181
# despite the ssh-agent process being missing.
181-
if [ ! -z $SSH_AGENT_PID ];
182+
if [ ! -z "$SSH_AGENT_PID" ];
182183
then
183184
echo "cleaning stale SSH_AGENT_PID: $SSH_AGENT_PID"
184185
unset SSH_AGENT_PID
185186
fi
186187

187-
if [ ! -z $SSH_AUTH_SOCK ];
188+
if [ ! -z "$SSH_AUTH_SOCK" ];
188189
then
189190
echo "cleaning stale SSH_AUTH_SOCK"
190191
unset SSH_AUTH_SOCK
191192
fi
192193

193-
if [ -e $_sockfile ];
194+
if [ -e "$_sockfile" ];
194195
then
195196
echo "removing stale socket file: $_sockfile"
196-
unlink $_sockfile
197+
unlink "$_sockfile"
197198
fi
198199
}
199200

0 commit comments

Comments
 (0)