有两种方法: 1.通过date命令获取,获取结果:0为星期日,1-6为星期一至星期六。 date +%w 取得当天是星期几 date -d 20120311 +%w 取得2012年3月11日是星期几 2.通过脚本获取 vim c_test.sh 创建脚本,输入以下所贴出的代码 chmod +x c_test.sh 给予脚本执行权限 ./c_test.sh 20120311 取得2012年3月11日是星期几 以下是该脚本的代码(其实通过这个脚本我们就可以看出从日期获取星期几的计算方法了):
#!/bin/bash # SAVE THIS CODE AS c_test.sh # # Uses Zellers Congruence calculation to use a date and give # the day of the week that date was. # # This function expects 1 Arguments, # YYYYMMDD # example:20120311 # then # Returns a value between 0 and 6 to represent the day of the # week where 0=Sun,1=Mon,...6=Sat # # e.g c_test.sh 20120311 # # This formula is Year 2000 compliant. # It is not compliant using dates previous to Oct 1752 # export YMD=$1 export YEAR=`echo $YMD|cut -c0-4` export MONTH=`echo $YMD|cut -c5-6` export DAY=`echo $YMD|cut -c7-9` # Adjust Month such that March becomes 1 month of # year and Jan/Feb become 11/12 of previous year # ============================================= if [ $MONTH -ge 3 ];then MONTH=`expr $MONTH - 2` else MONTH=`expr $MONTH + 10` fi if [ $MONTH -eq 11 ] || [ $MONTH -eq 12 ] ; then YEAR=`expr $YEAR - 1` fi # ============================================== # Split YEAR into YEAR and CENTURY # ================================ CENTURY=`expr $YEAR / 100` YEAR=`expr $YEAR % 100` # ================================ # Black Magic Time # ================ #Z=(( 26*$MONTH - 2 ) / 10) + $DAY + $YEAR + ( $YEAR/4 ) + ( $CENTURY/4 ) - (2 * $CENTURY) + 77) % 7 Z=`expr \( $MONTH \* 26 - 2 \) / 10` Z=`expr $Z + $DAY + $YEAR` Z=`expr $Z + $YEAR / 4` Z=`expr $Z + $CENTURY / 4` Z=`expr $Z - $CENTURY - $CENTURY + 77` Z=`expr $Z % 7` if [ $Z -lt 0 ] ; then Z=`expr $Z + 7` fi # ================ echo $Z # Sun 0 # Mon 1 # Tue 2 # Wed 3 # Thu 4 # Fri 5 # Sat 6 # ======================== 最后,想特别提醒一点的是,如果是采用crontab计划任务的方式来调用某个脚本执行,并希望在指定的星期几运行的话,直接通过crontab -e修改最后一个 * 号就可以实现,同样是0为星期天,1-6分别为星期一到星期六,不必劳神费力在脚本中判断当天是星期几了。 ----------- 各位大侠,如何生成2010-05-02 到2010-08-03?
2010-05-02
2010-05-03
2010-05-04
。。
。。
。。
2010-08-03 start=2010-05-02 end=2010-08-03 thisday=$start while [[ "$thisday" < "$end" ]] do echo $thisday thisday=`date -d "$thisday 1 days" "+%Y-%m-%d"` done #!/bin/bash
i=0 until [[ $day == "2010-08-03" ]] do day=$(date -d "2010-05-02 $i days" +%Y-%m-%d) echo $day ((i++)) done #!/bin/bash startDate=2010-05-02 endDate=2010-08-03 startSec=`date -d "$startDate" "+%s"` endSec=`date -d "$endDate" "+%s"` for((i=$startSec;i<=$endSec;i+=86400)) do date -d "@$i" "+%Y-%m-%d" done
|