Checking scripts on python scripts

I have the following shell script to request a python version. This gives me the error message "Integer expression expected" in the if-statement.

 #!/bin/bash PYTHON_VERSION=`python -c 'import sys; print("%i" % (sys.hexversion<0x03000000))'` echo $PYTHON_VERSION if [ $PYTHON_VERSION -eq 0 ] then echo "fine!" fi 

'echo $ PYTHON_VERSION' prints '0', so why don't the if statement work?

EDIT: I use Windows and Cygwin

+6
source share
4 answers
Good question. It works fine for me. You should always specify evaluated variables ( "$X" instead of $X ); perhaps this fixes your error.

But I suggest using the result of a python script instead of its output:

 #!/bin/bash if python -c 'import sys; sys.exit(1 if sys.hexversion<0x03000000 else 0)' then echo "Fine!" fi 

If you like to stay completely in the shell, you can also use the --version option:

 case "$(python --version 2>&1)" in *" 3."*) echo "Fine!" ;; *) echo "Wrong Python version!" ;; esac 

Perhaps this is more readable.

+6
source

The reason it doesn't work is because the result stored in $ PYTHON_VERSION is not an integer, so your equality test runs with two different types.

You can change if if:

 if [ $PYTHON_VERSION -eq "0" ]; then echo "fine!" fi 

or you can just do:

 if [ $PYTHON_VERSION = 0 ]; then 
+2
source

solution is possible here

 if sys.version_info < (3, 0): reload(sys) sys.setdefaultencoding('utf8') else: raw_input = input 

This is from the example that I had from sleekxmpp, but it does what you need. I suppose.

0
source

I don’t understand why this shit turns so stupid moth ** e *** e, this is for checking the contents of the filter

0
source

All Articles