You are not logged in.

#1 2012-01-20 14:16:29

graph
Member
Registered: 2010-12-21
Posts: 105

[Solved] python version and egrep

So, I'm building a very basic script to utilize the simple webserver in python. For the sake of portability, I would like it to recognise whether it's running on python 2.x or python 3.x.

Here's the problem:

user ~$ python --version | egrep -o '[0-9]\.[0-9]*\.[0-9]'
Python 3.2.2

Well, that's akward. Let's try with e.g. man to see if egrep works with other programs:

user ~$ man --version | egrep -o '[0-9]\.[0-9]*\.[0-9]'
2.6.0

How come python isn't behaving like all other programs when it comes to this?

Last edited by graph (2012-01-20 14:39:41)

Offline

#2 2012-01-20 14:22:44

lunar
Member
Registered: 2010-10-04
Posts: 95

Re: [Solved] python version and egrep

Use the following to directly print the major version of Python:

python -c 'import sys; print(sys.version_info[0])'

Or just run the Python script directly with Python 2 or Python 3 (depending on what version it needs) with "python2" and "python3" respectively.

Last edited by lunar (2012-01-20 14:24:44)

Offline

#3 2012-01-20 14:24:39

Blµb
Member
Registered: 2008-02-10
Posts: 224

Re: [Solved] python version and egrep

Because `python --version` prints to stderr, so you have to use:

python --version 2>&1 | egrep -o '[0-9]\.[0-9]*\.[0-9]'

You know you're paranoid when you start thinking random letters while typing a password.
A good post about vim
Python has no multithreading.

Offline

#4 2012-01-20 14:38:47

graph
Member
Registered: 2010-12-21
Posts: 105

Re: [Solved] python version and egrep

Blµb wrote:

Because `python --version` prints to stderr, so you have to use:

python --version 2>&1 | egrep -o '[0-9]\.[0-9]*\.[0-9]'

Haha I was so close! Except that I wrote it &2>1, which of course didn't work.

Thank you so much, I was about to go crazy for a minute here.

I even made a temporally fix, just to prove to my computer that I was still in charge:

ls -d /usr/lib/python* | egrep -o '[0-9].[0-9]*' | cut -d. -f1 | sort -n | tail -1

Last edited by graph (2012-01-20 14:41:37)

Offline

#5 2012-01-22 11:08:36

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [Solved] python version and egrep

If you're OK with parsing the output of ls, why not

ls -d /usr/lib/python* | cut -c 16 | tail -1

Offline

Board footer

Powered by FluxBB