真的值得通过 Python 询问sys
吗?
python -c "import sys; print sys.version_info[0:3]"
(2, 7, 3)
这样一来,除了检查版本之外,还可以检查版本值。
pyver=$(python -c "import sys; print sys.version_info[0:3]")
required="(2, 7, 1)"
echo pyver=$pyver
if [[ "$pyver" > "$required" ]] ; then
echo safe to proceed
else
echo require Python version ">" $required got $pyver
fi
required="(3, 0, 0)"
if [[ "$pyver" > "$required" ]] ; then
echo safe to proceed
else
echo require Python version ">" $required got $pyver
fi
#-- use integer values --
echo Integer version
# Trouble being the output looks odd but allows use of -gt
pyver=$(python -c "import sys; print '%03d%03d%03d' % sys.version_info[0:3]")
required="002007001"
echo pyver=$pyver
if [[ $pyver -gt $required ]] ; then
echo safe to proceed
else
echo require Python version $required got $pyver
fi
required="003000000"
if [[ $pyver -gt $required ]] ; then
echo safe to proceed
else
echo require Python version $required got $pyver
fi
使用 Python 2.7.3 版在我的系统上输出
pyver=(2, 7, 3)
safe to proceed
require Python version > (3, 0, 0) got (2, 7, 3)
Integer version
pyver=002007003
safe to proceed
require Python version 003000000 got 002007003