LInux shell 編寫記錄-日期9月判斷中遇到shell默認八進制計算

很高興能開始是新的知識點,我在使用sh 運行hive腳本以後才發現不知不覺的時候我已經開始使用shell了。。。

稀裏糊塗的就開始用shell了,希望我以後會好好系統的學習下。

———————————————————1—BUG—2020-03-18——————————————

    在shell中用到一個上月同期的日期情況,本來2020-03-05就直接上個月取到了2020-02-05,可是忽然想到了北部完遇到的二月份的那個閏月的上年沒有29號的問題,就想到13578十臘纔有31號,我就開始解決這個判斷:

export last_month_day="04-31" # 上月的月日 
export last_month_tq_day=31 # 上月的月日
if [[ $last_month_day -eq "02-30" ]]
then last_month_tq_day="30"
fi;
if [[ $last_month_day -eq "02-29" ]]
then last_month_tq_day="30"
fi;
if [[ $last_month_day -eq "04-31" ]]
then last_month_tq_day="30"
fi;
if [[ $last_month_day -eq "06-31" ]]
then last_month_tq_day="30"
fi;
if [[ $last_month_day -eq "09-31" ]]
then last_month_tq_day="30"
fi;
if [[ $last_month_day -eq "11-31" ]]
then last_month_tq_day="30"
fi;
echo $last_month_tq_day 

shell我暫時只會寫這中if結構,哈哈

按理說31號的問題解決了,但是,我一般都要逐個全部測試一次。。

注意:測到  export last_month_day="09-31" # 上月的月日 這句的時候,意外來了:

原來在shell中遇到 0開頭的數字,默認會以八進制來計算,也就是說到8時就會溢出,網上說可以強制 還用10#搞成10進制,不過我並不是要計算,我的解決方法:

export last_month_day="09-31" # 上月的月日 
export last_month_tq_day=31 # 上月的月日
if [[ $last_month_day -eq "02-30" ]]
then last_month_tq_day="30"
fi;
if [[ $last_month_day -eq "02-29" ]]
then last_month_tq_day="30"
fi;
if [[ $last_month_day -eq "04-31" ]]
then last_month_tq_day="30"
fi;
if [[ $last_month_day -eq "06-31" ]]
then last_month_tq_day="30"
fi;
# 特殊處理09
export jiu=${last_month_day:1:1}
if [[ $jiu -eq "9" ]] && [[ $last_month_tq_day -eq "31" ]]
then last_month_tq_day="30"
fi;
if [[ $last_month_day -eq "11-31" ]]
then last_month_tq_day="30"
fi;
echo $last_month_tq_day 

哈,偷個懶,解決。。。

 

——————————————END—————————————————

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章