SHELL 常用自定义函数

#获取特殊ASCII不可见字符
GetAscii(){
if [ $# -ne 1 -o $1 -lt 0 -o $1 -gt 127 ]
then
echo "######################################"
echo "USAGE:"
echo " GetAscii NUM "
echo " NUM NUM must great or equal 0 "
echo " and NUM less or equal 127"
echo "######################################"
else
awk -v char=$1 'BEGIN {printf "%c",char;exit}'
fi
}

#得到前一天日期

GetPrevDate(){

if [ $# -eq 0 ];then

CurDate=`date +%Y%m%d`

else

CurDate=$1

fi

CurYear=`echo $CurDate | cut -c1-4`

CurMonth=`echo $CurDate | cut -c5-6`

CurDay=`echo $CurDate | cut -c7-8`

 

GetYear="$CurYear"

GetMonth="$CurMonth"

GetDay="`expr $CurDay -1`"

 

if [ "$GetDay" -le 0 ];then

GetMonth=`expr $CurMonth -1`

if [ "$GetMonth" -le 0 ];then

GetYear=`expr $CurYear -1`

GetMonth=12

fi

case "$GetMonth"

in

1|3|5|7|8|10|12)

GetDay=31;;

4|6|9|11)

GetDay=30;;

2)

if [ `expr "$CurYear" %400` -eq 0 ];then

GetDay=29

elif [ `expr "$CurYear" %4` -eq 0 -a `expr "$CurYear" %100` -ne 0];then

GetDay=29

else

GetDay=28

fi

esac

fi

if [ `echo "$GetMonth" | wc -m` -ne 3 ];then

GetMonth=0$GetMonth

fi

fi [ `echo "$GetDay" | wc -m` -ne 3 ];then

GetDay=0$GetDay

fi

echo "$GetYear""$GetMonth""$GetDay"

 

}

 

#得到后一天日期
GetNextDate()
{
if (test $# -eq 0)
then
CurDate=`date +%Y%m%d`
else
CurDate=$1
fi

CurYear=`echo $CurDate | cut -c1-4`
CurMonth=`echo $CurDate | cut -c5-6`
CurDay=`echo $CurDate | cut -c7-8`

GetYear="$CurYear"
GetMonth="$CurMonth"
GetDay="`expr $CurDay + 1`"


case "$GetMonth"
in
01|03|05|07|08|10|12)
MaxDay=31;;
04|06|09|11)
MaxDay=30;;
02)

if [ `expr "$CurYear" % 400` -eq 0 ]; then

MaxDay=29

elif [ `expr "$CurYear" % 4` -eq 0 -a `expr "$CurYear" % 100` -ne 0 ]; then

MaxDay=29

else
MaxDay=28
fi
esac

 


if [ "$GetDay" -gt "$MaxDay" ]; then

GetMonth=`expr $CurMonth + 1`
GetDay=01

if [ $GetMonth -gt 12 ] ; then

GetYear=`expr $CurYear + 1`
GetMonth=01

fi

 

fi


if [ `echo "$GetMonth" | wc -m` -ne 3 ] ; then

GetMonth=0$GetMonth
fi

if [ `echo "$GetDay" | wc -m` -ne 3 ] ; then

GetDay=0$GetDay
fi

echo "$GetYear""$GetMonth""$GetDay"
}

#得到上一个月份
GetLastMonth_1st()
{
yearName=`date +%Y`
monthName=`date +%m`

if [ ${monthName} -eq 1 ] ; then
yearName=`expr ${yearName} - 1`
monthName=12
else
monthName=`expr ${monthName} - 1`
monthName=`printf "%.2d" ${monthName}`
fi
echo ${yearName}${monthName}
}

#当前日期戳
GetTime(){
echo `date +%Y`"年 "`date +%m`"月"`date +%d`"日 "`date +%H`":"`date +%M`":"`date +%S`
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值