×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

chao lai de.

本文发表在 rolia.net 枫下论坛#!/bin/sh

# ydate: A Bourne shell script that
# prints yestarday's date
# Output Form: Month Day Year
# From Focus on Unix: http://unix.about.com

# Set the current month day and year.
month=`date +%m`
day=`date +%d`
year=`date +%Y`

# Add 0 to month. This is a
# trick to make month an unpadded integer.
month=`expr $month + 0`

# Subtract one from the current day.
day=`expr $day - 1`

# If the day is 0 then determine the last
# day of the previous month.
if [ $day -eq 0 ]; then

# Find the preivous month.
month=`expr $month - 1`

# If the month is 0 then it is Dec 31 of
# the previous year.
if [ $month -eq 0 ]; then
month=12
day=31
year=`expr $year - 1`

# If the month is not zero we need to find
# the last day of the month.
else
case $month in
1|3|5|7|8|10|12) day=31;;
4|6|9|11) day=30;;
2)
if [ `expr $year % 4` -eq 0 ]; then
if [ `expr $year % 400` -eq 0 ]; then
day=29
elif [ `expr $year % 100` -eq 0 ]; then
day=28
else
day=29
fi
else
day=28
fi
;;
esac
fi
fi

# Print the month day and year.
echo $month $day $year
exit 0更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / IT杂谈 / 一个unix shell script 技术问题(可能是很好的interview问题呦),不能用perl. 用"date" 命令可以得到今天的日期,如 Aug 21,但如何得到昨天的日期,或者一个星期以前的日期呢?
    • man date, there is probably option letting you input N days ago.
      • I've checked, still no clue.
    • 我的TIMEX手表上有日期.
      • 可喜丢了,赫赫
        • 那是CASIO, 你不提我都想不起了...个坏蛋.
    • On my FreeBSD, I can use 'date -v-1d' to get yesterday's date. But I can't figure out how to use date to do it with Linux or Solaris. But
      of course in the shell script you can use date to get today's date first and then calculate yesterday's date.
      • "of course in the shell script you can use date to get today's date first and then calculate yesterday's date. " How? can I do it within 3 lines?
        • Which OS? It's easy with BSD or Linux. I still can't figure out Solaris. Or use what noexit posts (well, that's much more than 3 lines but it works).
          • solaris
            • Solaris' strftime(3C) does not have '%s', 'date' does not have '-r', otherwise it would be much easier.
    • chao lai de.
      本文发表在 rolia.net 枫下论坛#!/bin/sh

      # ydate: A Bourne shell script that
      # prints yestarday's date
      # Output Form: Month Day Year
      # From Focus on Unix: http://unix.about.com

      # Set the current month day and year.
      month=`date +%m`
      day=`date +%d`
      year=`date +%Y`

      # Add 0 to month. This is a
      # trick to make month an unpadded integer.
      month=`expr $month + 0`

      # Subtract one from the current day.
      day=`expr $day - 1`

      # If the day is 0 then determine the last
      # day of the previous month.
      if [ $day -eq 0 ]; then

      # Find the preivous month.
      month=`expr $month - 1`

      # If the month is 0 then it is Dec 31 of
      # the previous year.
      if [ $month -eq 0 ]; then
      month=12
      day=31
      year=`expr $year - 1`

      # If the month is not zero we need to find
      # the last day of the month.
      else
      case $month in
      1|3|5|7|8|10|12) day=31;;
      4|6|9|11) day=30;;
      2)
      if [ `expr $year % 4` -eq 0 ]; then
      if [ `expr $year % 400` -eq 0 ]; then
      day=29
      elif [ `expr $year % 100` -eq 0 ]; then
      day=28
      else
      day=29
      fi
      else
      day=28
      fi
      ;;
      esac
      fi
      fi

      # Print the month day and year.
      echo $month $day $year
      exit 0更多精彩文章及讨论,请光临枫下论坛 rolia.net
      • SHIT!! 这也叫答案?对不起,不是冲你的,只是觉得shell script 太愚蠢了,这样的功能都没做个命令?
        • unix嘛, 嘿嘿..
    • Search comp.unix.shell group on the google
      • 看来这是一个很著名的问题,怪不得我想不出来.....GNU 的 date是个解决方案。solaris上有吗?
    • During the interview. Simply tell the interviewer to get the julian date and minus one and convert it to calender date. Maybe the interviewer doesn't know the answer, either.
    • 答案: YESTERDAY=`/usr/local/gnu/bin/gdate -d "1 day ago" +20%y-%m-%d`
      • no gdate installed in my rh 7.3..:D