I have a script.sh
file which checks for loaded SSH agent and adds a key.
If I run this script directly, it works but if I run it via some worker it doesn't unless I do those changes:
This works:
#!/bin/bash -e
printf "<<<<< Start SSH agent and Github deploy key >>>>>\n"
if ps -p $SSH_AGENT_PID > /dev/null
then
printf "<<<<< ssh-agent is already running >>>>>\n"
else
eval `ssh-agent -s`
fi
ssh-add $deploy_key_path
But his doesn't work:
#!/bin/bash -e
if [ $(ps ax | grep [s]sh-agent | wc -l) -gt 0 ] ; then
printf "<<<<< ssh-agent is already running >>>>>\n"
else
eval `ssh-agent -s`
fi
ssh-add $deploy_key_path
The error says ...failed. Exit Code: 2(Misuse of shell builtins)..
which happens at the line ssh-add $deploy_key_path
When checking the reserved Bash error codes I see:
2 Misuse of shell builtins empty_function() {} Missing keyword or command
eval
line.eval
withecho
to see what it's trying to evaluate.grep ... | wc -l
can be combined intogrep -c ...
deploy_key_path
? If it's set byeval
, then the line that uses it should be in theelse
.