-1

I have a folder that will have a file name based on the python version being used.

PyICU-1.9.5-cp27-cp27m-macosx_10_6_x86_64.whl
PyICU-1.9.5-cp35-cp35m-macosx_10_6_x86_64.whl

I want to use bash to detect the 35 or 27, and then execute a pip install <filename>. I have GitHub Gist that has this hard coded, but instead of Hard coding the filename, I would like to detect the version, pick the filename with that version, and use it in a new command.

I would like to change the pip install command based on the file name. I found some code to detect the python version; what's the next piece?

#from http://stackoverflow.com/questions/6141581/detect-python-version-in-shell-script
python -c 'import sys; print(sys.version_info[:])'

How do I detect the numbers after cp, so that I can construct this: detected 34, so: pip installPyICU-1.9.5-cp34-cp34-macosx_10_6_x86_64.whl

Linwoodc3
  • 1,037
  • 2
  • 11
  • 14

3 Answers3

0
#!/bin/bash
#Assuming $ver holds the version string...
ver=$(echo "$ver" | awk -F - '{print $3}' | sed 's/^..//')

For a more detailed explanation;

awk -F - '{print $3}' uses - as the field separator and returns the third field. The third field in this example is returned as cp34.

sed 's/^..//' removes the first two characters, and returns the rest, in this case 34.

Guest
  • 124
  • 7
0

Since you don't even need to extract the version from the filename, but just execute the file that is in the folder, then you can do:

cd dist/ &&
filename=$(ls|grep 'PyICU-')
pip install $filename
codeforwin
  • 51
  • 3
0
python -c 'import sys; print(sys.version_info[:])'

This yields e. g. (2, 7, 3, 'final', 0), while what you need for the file name would be just 27, so we have to modify the above code a little:

python -c 'import sys; print("%s%s"%sys.version_info[:2])'

what's the next piece?

Now we can set a variable to the output of the above and use it in the file name:

ver=`python -c 'import sys; print("%s%s"%sys.version_info[:2])'`
pip installPyICU-1.9.5-cp${ver}-cp${ver}-macosx_10_6_x86_64.whl
Armali
  • 18,255
  • 14
  • 57
  • 171