随便用shell写了下,其实应该可以用date来做,但我这里用拼接字符串来写了。
#!/bin/sh
function get_last_day(){
year=`expr substr $1 1 4`
month=`expr substr $1 5 2`
if [ $month = '01' ] || [ $month = '03' ] || [ $month = '05' ] || [ $month = '07' ] || [ $month = '08' ] || [ $month = '10' ] || [ $month = '12' ] ; then
echo $year''$month''31
elif [ $month = '02' ] ; then
if [ `expr $year % 400` = 0 ] ; then
echo $year''$month''29
elif [ `expr $year % 4` = 0 ] && [ `expr $year % 100` != 0 ] ; then
echo $year''$month''29
else
echo $year''$month''28
fi
else
echo $year''$month''30
fi
}
|
注意几点:
1) if语句的[]里的写法是
所有单词和符号之间都留空格,if[a=1]是错的,应该是if [ a = 1 ]2) shell中if里面如果要多条件判断,if [ a = 1 || a = 2]这样写是错的,应该是if [ a = 1 ] || [ a = 2 ]
3)
echo $year''$month''29 这里''不能省略,不然只会输出年字段
阅读(6008) | 评论(0) | 转发(0) |