Reputation: 1047
So this may have been a dumb way of doing it but I wanted to check if pip was installed via a script, so I ran the pip version command and checked if the first 3 characters of the command were "pip" and if so I could say I knew pip was installed. Problem is when I run this and get a command not found
error, the logic still thinks pip is installed. Why is? What is that command not found
error returning? It can't be pip right so why does pip not install? And what's a better way of detecting if pip is installed?
pip=$(sudo /root/.local/bin/pip -V | cut -c 1-3)
if [[ pip != "pip" ]]; then
echo "Installing pip..."
curl https://bootstrap.pypa.io/get-pip.py -o /tmp/get-pip.py
sudo /workplace/user/package/src/EnvImprovement/bin/python2.7 get-pip.py --user
else
echo "Pip detected."
fi
Upvotes: 1
Views: 3319
Reputation: 281252
You're testing whether the string pip
is equal to the string pip
, which it is. The pip
variable isn't involved.
Unlike in Python, pip
and "pip"
are equivalent in a shell script. The quotes in "pip"
suppress special interpretation of a number of characters, none of which are actually present in pip
anyway. You need $pip
to perform variable expansion.
Even then, hardcoding /root/.local/bin/pip
doesn't make much sense, and executing pip by a hardcoded path to determine whether it exists seems like a strange idea compared to testing whether an executable exists at that path, or taking a different approach altogether.
You should probably use command -v pip
instead of your current approach.
Upvotes: 1