首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

SHELL 惯用自定义函数

2012-08-14 
SHELL 常用自定义函数#获取特殊ASCII不可见字符GetAscii(){if [ $# -ne 1 -o $1 -lt 0 -o $1 -gt 127 ]the

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`
}

热点排行