1、下载安装busybox:
复制代码
wget http://busybox.net/downloads/busybox-1.29.3.tar.bz2
tar -jxvf busybox-1.29.3.tar.bz2
cd busybox-1.29.3
make defconfig //如果对根文件系统的大小不是很苛求,可以直接使用busybox的默认配置
make
make install
构建date链接
ln -sf ./busybox ./date
复制代码
busybox date参数详解
复制代码
[busybox-1.29.3]# ./date --help
BusyBox v1.29.3 (2019-11-25 11:00:35 CST) multi-call binary.
Usage: date [OPTIONS] [+FMT] [TIME]
Display time (using +FMT), or set time
[-s,--set] TIME Set time to TIME
-u,--utc Work in UTC (don't convert to local time)
-R,--rfc-2822 Output RFC-2822 compliant date string
-I[SPEC] Output ISO-8601 compliant date string
SPEC='date' (default) for date only,
'hours', 'minutes', or 'seconds' for date and
time to the indicated precision
-r,--reference FILE Display last modification time of FILE
-d,--date TIME Display TIME, not 'now'
-D FMT Use FMT for -d TIME conversion
Recognized TIME formats:
hh:mm[:ss]
[YYYY.]MM.DD-hh:mm[:ss]
YYYY-MM-DD hh:mm[:ss]
[[[[[YY]YY]MM]DD]hh]mm[.ss]
‘date TIME’ form accepts MMDDhhmm[[YY]YY][.ss] instead
复制代码
Linux系统中的date一般可以直接进行日期的相减,
例如: centos7系统
date 获取前一天的时间: date -d -1day 或者 date -d ‘1 day ago’
date 获取前一个月的时间: date -d ‘1 month ago’
date 获取前一年的时间: date -d ‘1 year ago’
而busybox date则不可以直接获取前一天的时间,,需要时间数字相减的方法来实现获取前一天的时间
例如: busybox date 获取前一天的时间:
考虑到当前时间是年度第一天1月1日的情况,代码如下:
复制代码
year=date +%Y
// 获取当前时间的年份
month=date +%m
// 获取当前时间的月份
day=date +%d
// 获取当前时间的日期
if [[ “day"=="01"]];then//如果当前时间是1号,则考虑一下月份问题if[["day" == "01" ]];then // 如果当前时间是1号 ,则考虑一下月份问题
if [[ "day"=="01"]];then//如果当前时间是1号,则考虑一下月份问题if[["month” == “01” ]];then // 如果当前时间是1月1号,,获取的前一天则是去年的最后一天 ,所以年份需要减一,月份和日期则是12月31日
year=expr $year - 1
yesterday=“year−12−31"elif[["{year}-12-31"
elif [[ "year−12−31"elif[["month” == “03” ]];then // 如果当前时间是3月1号,获取的前一天则是2月的最后一天,2月又分28天和29天,,所以需要和4取余,
year_type=expr ${year} % 4
if [[ “yeartype"=="0"]];then//与4取余为0则为闰年,这一年的2月最后一天是29号,,取余不为0则为平年,2月的最后一天是28号day="29"elseday="28"fiyesterday="year_type" == "0" ]];then // 与4取余为0则为闰年,这一年的2月最后一天是29号,,取余不为0则为平年,2月的最后一天是28号
day="29"
else
day="28"
fi
yesterday="yeartype"=="0"]];then//与4取余为0则为闰年,这一年的2月最后一天是29号,,取余不为0则为平年,2月的最后一天是28号day="29"elseday="28"fiyesterday="{year}-02-day"//1,3,5,7,8,10,12月均是31天elif[["{day}"
// 1,3,5,7,8,10,12月均是31天
elif [[ "day"//1,3,5,7,8,10,12月均是31天elif[["month” == “02” || “month"=="04"∣∣"month" == "04" || "month"=="04"∣∣"month” == “06” || “month"=="08"∣∣"month" == "08" || "month"=="08"∣∣"month” == “09” || “$month” == “11” ]];then
month=expr ${month} - 1
yesterday="year−{year}-year−{month}-31"
elif [[ “month"=="05"∣∣"month" == "05" ||"month"=="05"∣∣"month” == “07” || “month"=="10"∣∣"month" == "10" || "month"=="10"∣∣"month” == “12” ]];then
month=expr ${month} - 1
yesterday="year−{year}-year−{month}-30"
fi
else
yesterday=year−{year}-year−{month}-expr ${day} - 1
fi
echo $yesterday
复制代码
努力成为一名优秀的工程师