shell scripting checking python version
Good question. For me it's working fine. You always should quote evaluated variables ("$X"
instead of $X
); maybe that fixes your error.
But I propose to use the result of the 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 in the shell completely, you also can use the --version
option:
case "$(python --version 2>&1)" in
*" 3."*)
echo "Fine!"
;;
*)
echo "Wrong Python version!"
;;
esac
Maybe that's more readable.
The reason why it doesn't work is because the result stored in $PYTHON_VERSION is not an integer, so your equality test is being done with two different types.
You can change the if to:
if [ $PYTHON_VERSION -eq "0" ]; then
echo "fine!"
fi
or you can just do:
if [ $PYTHON_VERSION = 0 ]; then