Please teach how to get the current system time in the shell script
In the shell script often need to get the system time to deal with a certain operation, today the system to learn how to get the system time. The record is as follows:
linux system time in the shell can be directly called system variables such as:
Get today’s period: `date+%Y%m%d` or `date+%F` or $(date+%y%m%d)
Command output results are as follows:
[root@centi- Csh]#date+%Y%m%d
20120727
[root@centi-Csh]#date+%F
2012-07-27
[root@centi-Csh]#date+%y%m%d
120727
Get yesterday’s period: `date-dyesterday+%Y%m%d` can also be written as `date-d-1day+%Y%m%d`
[root@centi-Csh]#date-dyesterday+%Y%m%d
20120726
[ root@centi-Csh]#date-dyesterday+%F
2012-07-26
[root@centi-Csh]#date-d-1day+%y%m%d
120726
[root@centi-Csh]# date-d-1day+%Y%m%d
20120726
Get the previous day’s date: `date-d-2day+%Y%m%d`
And so on e.g. to get the date of 10 days ago: `date-d-10day+%Y%m%d`
Or n days ago `date-d “ndaysago “+%y%m%d`
Tomorrow: `date-dtomorrow+%y%m%d`
Note that there is a space in the middle of the above
As to what kind of datetime format you need, you will need to apply the relevant time field parameter to realize it
Related time fields are as follows:
%H
%H hours (00..23)
%I hours (01..12)
%k hours (0..23)
%l hours (1..12)
%M minutes (00..59)
%p displays either AM or PM
%r time (hh:mm:ssAM or PM), 12 hours
%s the number of seconds elapsed from 00:00:00 on January 1, 1970 to the present
%S seconds (00..59)
%T time (24-hour format) (hh:mm:ss)
%X the format of the time displayed (%H:%M:%S)
%Z the time zone date field
%aShortened name of the day of the week (Sun..Sat)
%AFull name of the day of the week (Sunday..Saturday)
%bShortened name of the month (Jan..Dec)
%BFull name of the month (January..December)
%cDate and time ( MonNov814:12:46CST1999)
%d the first day of the month (01..31)
%D the date (mm/dd/yyy)
%h and %b the same options
%j the first day of the year (001..366)
%m the month (01..12)
%wThe first day of the week (0 for Sunday)
%WThe first week of the year (00..53, with Monday being the first day)
%xThe format of the date displayed (mm/dd/yyy)
%YThe last two digits of the year (99 for 1999)
%YThe year (e.g., 1970. 1996, etc.
Note: Only the superuser has permission to set the time using the date command; the general user can only display the time using the date command.
Add a practice script that does the following:
Backs up and compresses the contents of the /etc directory on the first day of each month and stores it in the /root/bak directory with the following filename yyymmdd_etc, where yy is the year, mm is the month, and dd is the day. The shell program fileback is stored in the /usr/bin directory.
#/bin/bash
#filebak
#fileexecutable:chmod755filebak
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/ usr/local/sbin:~/bin
exportPATH
bakdir=”/root/bak/”
filename=”`date+%y%m%d`_etc.tar.gz”
if[! -x”$bakdir”];then
mkdir$bakdir
fi
cd$bakdir
tarcvfz$filename/etc
Or add a timed task using the crontab-e command:
01***/ bin/sh/usr/bin/fileback
How to get the current date and time in the shell
Get the current day’s date: [root@master ~]# date +%Y-%m-%d
Sometimes you need to use a date before today or a date after today, and this can be done with the -d parameter of date.
Get tomorrow’s date: date -d next-day +%Y%m%d
Get yesterday’s date: date -d last-day +%Y%m%d
Get the year and month of the previous month: date -d last-month +%Y%m
Get the year and month of the next month: date -dnext-month +%Y%m
Get next year’s year: date -dnext-year +%Y
Expanded Information
Time Field Parameters:
Time Field Parameters are used to format the output date, and the related time field parameters are as follows:
%H hours ( 00…23)
%I hours (01…12)
%k hours (0…23)
%l hours (1…12)
%M minutes (00 .59)
%p shows AM or PM
%r time (hh:mm:ssAM or PM), 12 hours
%s number of seconds elapsed from January 1, 1970, 00:00:00 to the present
%S seconds (00..59) p>
How can a shell script execution log be timestamped
Wrap a shelllib that writes a log and define the following function
#! /bin/sh
functionwrite_log()
{
locallogType=$1
locallogMsg=$2
locallogName=$3
echo”$logType:` date+%Y-%m-%d\%T`:$logMsg”>>$logName
}
#Call it as follows, you can see that the date has been printed in the log.txt
write_logFATAL “somethingerror “log.txt