2

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.

4
  • 1
    think it would be, if [[ "$PYTHON" == 'Python 2.7.6' ]] Commented May 15, 2015 at 4:49
  • @AvinashRaj, I thought that correcting the if would solve the case, but it actually didn't: python -V prints the version just fine but it returns an empty result. So the variable is always empty. Commented May 15, 2015 at 5:30
  • you want to check the existence of python or this particular version of python? Commented May 15, 2015 at 15:18
  • This particular version. Commented May 15, 2015 at 16:22

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

sys.version_info would be better, since you can access various components of the version number in a controlled way.
The first answer you gave achieved exactly what was needed. Thank you.
0

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

6 Comments

While this lets me know that Python is installed, I need to know the specific version installed. I am creating the script to automate the installation process of Jekyll for Windows users and Python 2.7.* works where Python 3.* does not.
@Jahid your explanation of exit and what gets fed to the bash "capture standard output and assign it to a variable" construct (i.e. $(...)) is incorrect.
don't use which to check command existence: stackoverflow.com/questions/592620/…
Can you explain a little, @twalberg
@Jahid PYTHON=$(...) captures the standard output of a program, which, by token of the fact that ${PYTHON} was ending up empty, demonstrates the fact that python -V in fact produces no output on standard output (it goes to standard error, instead). The exit library call returns an exit code to the shell, which is a different concept than writing something to the output. This is somewhat confused by the fact that Python provides an overloaded exit() call such that exit('foo') is roughly equivalent to print 'foo'; exit()...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.