今天写了一个shell函数来查编译环境的python版本,感觉还是学到了不少新东西,简单记录一下,能写出一点东西满足自己的需求也是一件开心的事情。
1. awk果然强大,本来也没有用过,上网了解了一些基本用法,解决了我的需求
2. SHELL里比较浮点数的大小确实很麻烦,还好这次小数点后只有两位。
#!/bin/sh checkPython() { #推荐版本V2.6.5 V1=2 V2=6 V3=5 echo need python version is : $V1.$V2.$V3 #获取本机python版本号。这里2>&1是必须的,python -V这个是标准错误输出的,需要转换 U_V1=`python -V 2>&1|awk '{print $2}'|awk -F '.' '{print $1}'` U_V2=`python -V 2>&1|awk '{print $2}'|awk -F '.' '{print $2}'` U_V3=`python -V 2>&1|awk '{print $2}'|awk -F '.' '{print $3}'` echo your python version is : $U_V1.$U_V2.$U_V3 if [ $U_V1 -lt $V1 ];then echo 'Your python version is not OK!(1)' exit 1 elif [ $U_V1 -eq $V1 ];then if [ $U_V2 -lt $V2 ];then echo 'Your python version is not OK!(2)' exit 1 elif [ $U_V2 -eq $V2 ];then if [ $U_V3 -lt $V3 ];then echo 'Your python version is not OK!(3)' exit 1 fi fi fi echo Your python version is OK! } checkPython