今天寫了一個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