Reputation: 23
I am creating an install script where I would like to compare the version of installed default Python with the version I need to have running. Currently here is my code:
#!/bin/bash
PYTHON="$(python -V)"
if [[ "$PYTHON = 'Python 2.7.6' ]]
then echo "Python is installed."
else echo "Python is not installed."
fi
The response I keep getting is that Python is not installed, but that is the output when I type in the command python -V.
Any help is greatly appreciated. Thank you in advance.
Upvotes: 2
Views: 2132
Reputation: 22428
You can change your code like this:
#!/bin/bash
PYTHON="$(python -V 2>&1)"
if [[ "$PYTHON" = "Python 2.7.6" ]]; then
echo "Python is installed."
else
echo "Python is not installed."
fi
Upvotes: 0
Reputation: 62369
It seems that when you run python -V
, it prints the version to stderr, not to stdout. So, changing your attempt to capture the output into a variable to this:
PYTHON=$(python -V 2>&1)
should do the trick. Another alternative, which tends to include additional information about build dates, compilers, etc. would be:
python -c 'import sys; print sys.version'
or, as suggested by @chepner:
python -c 'import sys; print sys.version_info'
Both of these would require a little additional parsing to get the specific information you want/need, though.
Upvotes: 3