茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1672562 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
LSxMF_00018.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

小企鵝開談 : [轉貼]linux 中用 shell 獲取昨天、明天或多天前的日期

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]linux 中用 shell 獲取昨天、明天或多天前的日期
linux中用shell獲取昨天、明天或多天前的日期
日期:2009-10-23

linux中用shell獲取昨天、明天或多天前的日期:

在Linux中對man date -d 參數說的比較模糊,以下舉例進一步說明:
# -d, --date=STRING display time described by STRING, not `now
[root@Gman root]# date -d next-day +%Y%m%d #明天日期
20091024
[root@Gman root]# date -d last-day +%Y%m%d #昨天日期
20091022
[root@Gman root]# date -d yesterday +%Y%m%d #昨天日期
20091022
[root@Gman root]# date -d tomorrow +%Y%m%d # 明天日期
20091024
[root@Gman root]# date -d last-month +%Y%m #上個月日期
200909
[root@Gman root]# date -d next-month +%Y%m #下個月日期
200911
[root@Gman root]# date -d next-year +%Y #明年日期
2010

DATE=$(date +%Y%m%d --date  2 days ago ) #獲取昨天或多天前的日期

名稱 : date
使用權限 : 所有使用者
使用方式 : date [-u] [-d datestr] [-s datestr] [--utc] [--universal] [--date=datestr] [--set=datestr] [--help] [--version] [+FORMAT] [MMDDhhmm[[CC]YY][.ss]]
說明 : date 能用來顯示或設定系統的日期和時間,在顯示方面,使用者能設定欲顯示的格式,格式設定為一個加號後接數個標記,其中可用的標記列表如下 :
時間方面 :
% : 印出
% %n : 下一行
%t : 跳格
%H : 小時(00..23)
%I : 小時(01..12)
%k : 小時(0..23)
%l : 小時(1..12)
%M : 分鐘(00..59)
%p : 顯示本地 AM 或 PM
%r : 直接顯示時間 (12 小時制,格式為 hh:mm:ss [AP]M)
%s : 從 1970 年 1 月 1 日 00:00:00 UTC 到目前為止的秒數 %S : 秒(00..61)
%T : 直接顯示時間 (24 小時制)
%X : 相當於 %H:%M:%S
%Z : 顯示時區
日期方面 :
%a : 星期幾 (Sun..Sat)
%A : 星期幾 (Sunday..Saturday)
%b : 月份 (Jan..Dec)
%B : 月份 (January..December)
%c : 直接顯示日期和時間
%d : 日 (01..31)
%D : 直接顯示日期 (mm/dd/yy)
%h : 同 %b
%j : 一年中的第幾天 (001..366)
%m : 月份 (01..12)
%U : 一年中的第幾周 (00..53) (以 Sunday 為一周的第一天的情形)
%w : 一周中的第幾天 (0..6)
%W : 一年中的第幾周 (00..53) (以 Monday 為一周的第一天的情形)
%x : 直接顯示日期 (mm/dd/yy)
%y : 年份的最後兩位數字 (00.99)
%Y : 完整年份 (0000..9999)
若是不以加號作為開頭,則表示要設定時間,而時間格式為 MMDDhhmm[[CC]YY][.ss],
其中 MM 為月份,
DD 為日,
hh 為小時,
mm 為分鐘,
CC 為年份前兩位數字,
YY 為年份後兩位數字,
ss 為秒數
把計 :
-d datestr : 顯示 datestr 中所設定的時間 (非系統時間)
--help : 顯示輔助訊息
-s datestr : 將系統時間設為 datestr 中所設定的時間
-u : 顯示目前的格林威治時間
--version : 顯示版本編號
例子 :
顯示時間後跳行,再顯示目前日期 : date +%T%n%D
顯示月份和日數 : date +%B %d
顯示日期和設定時間(12:34:56) : date --date 12:34:56
設置系統當前時間(12:34:56):date --s 12:34:56
注意 : 當你不希望出現無意義的 0 時(比如說 1999/03/07),則能在標記中插入 - 符號,比如說 date +%-H:%-M:%-S 會把時分秒中無意義的 0 給去掉,像是原本的 08:09:04 會變為 8:9:4。另外,只有取得權限者(比如說 root)才能設定系統時間。 當你以 root 身份更改了系統時間之後,請記得以 clock -w 來將系統時間寫入 CMOS 中,這樣下次重新開機時系統時間才會持續抱持最新的正確值。
ntp時間同步
linux系統下默認安裝了ntp服務,手動進行ntp同步如下
ntpdate ntp1.nl.net

當然,也能指定其他的ntp服務器
-------------------------------------------------------------------
擴展功能
date 工具可以完成更多的工作,不僅僅只是打印出當前的系統日期。您可以使用它來得到給定的日期究竟是星期幾,並得到相對於當前日期的相對日期。瞭解某一天是星期幾
GNU 對 date 命令的另一個擴展是 -d 選項,當您的桌上沒有日曆表時(UNIX 用戶不需要日曆表),該選項非常有用。使用這個功能強大的選項,通過將日期作為引號括起來的參數提供,您可以快速地查明一個特定的日期究竟是星期幾:
$ date -d "nov 22"
Wed Nov 22 00:00:00 EST 2006
$

在本示例中,您可以看到今年的 11 月 22 日是星期三。
所以,假設在 11 月 22 日召開一個重大的會議,您可以立即瞭解到這一天是星期三,而這一天您將趕到駐地辦公室。
獲得相對日期
d 選項還可以告訴您,相對於 當前日期若干天的究竟是哪一天,從現在開始的若干天或若干星期以後,或者以前(過去)。通過將這個相對偏移使用引號括起來,作為 -d 選項的參數,就可以完成這項任務。
例如,您需要瞭解兩星期以後的日期。如果您處於 Shell 提示符處,那麼可以迅速地得到答案:
$ date -d 2 weeks

關於使用該命令,還有其他一些重要的方法。使用 next/last指令,您可以得到以後的星期幾是哪一天:
$ date -d  next monday  (下週一的日期)
$ date -d next-day +%Y%m%d(明天的日期)或者:date -d tomorrow +%Y%m%d
$ date -d last-day +%Y%m%d(昨天的日期) 或者:date -d yesterday +%Y%m%d
$ date -d last-month +%Y%m(上個月是幾月)
$ date -d next-month +%Y%m(下個月是幾月)

使用 ago 指令,您可以得到過去的日期:
$ date -d  30 days ago  (30天前的日期)

您可以使用負數以得到相反的日期:
$ date -d ’dec 14 -2 weeks  (相對:dec 14這個日期的兩周前的日期)
$ date -d  -100 days  (100天以前的日期)
$ date -d  50 days (50天後的日期)

這個技巧非常有用,它可以根據將來的日期為自己設置提醒,可能是在腳本或 Shell 啟動文件中,如下所示:
DAY=`date -d  2 weeks  +"%b %d"`
if test "`echo $DAY`" = "Aug 16"; then echo  Product launch is now two weeks away! ; fi



原文出處:linux中用shell获取昨天、明天或多天前的日期-php自学网
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[分享]鳥哥的隨日期變化:利用 date 進行檔案的建立
隨日期變化:利用 date 進行檔案的建立

想像一個狀況,假設我的伺服器內有資料庫,資料庫每天的資料都不太一樣,因此當我備份時, 希望將每天的資料都備份成不同的檔名,這樣才能夠讓舊的資料也能夠保存下來不被覆蓋。 哇!不同檔名呢!這真困擾啊?難道要我每天去修改 script ?

不需要啊!考慮每天的『日期』並不相同,所以我可以將檔名取成類似: backup.2009-02-14.data , 不就可以每天一個不同檔名了嗎?呵呵!確實如此。那個 2009-02-14 怎麼來的?那就是重點啦!接下來出個相關的例子: 假設我想要建立三個空的檔案 (透過 touch) ,檔名最開頭由使用者輸入決定,假設使用者輸入 filename 好了,那今天的日期是 2009/02/14 , 我想要以前天、昨天、今天的日期來建立這些檔案,亦即 filename_20090212, filename_20090213, filename_20090214 ,該如何是好?
[root@www scripts]# vi sh03.sh
#!/bin/bash
# Program:
#	Program creates three files, which named by user's input
#	and date command.
# History:
# 2005/08/23	VBird	First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# 1. 讓使用者輸入檔案名稱,並取得 fileuser 這個變數;
echo -e "I will use 'touch' command to create 3 files." # 純粹顯示資訊
read -p "Please input your filename: " fileuser         # 提示使用者輸入

# 2. 為了避免使用者隨意按 Enter ,利用變數功能分析檔名是否有設定?
filename=${fileuser:-"filename"}           # 開始判斷有否設定檔名

# 3. 開始利用 date 指令來取得所需要的檔名了;
date1=$(date --date='2 days ago' +%Y%m%d)  # 前兩天的日期
date2=$(date --date='1 days ago' +%Y%m%d)  # 前一天的日期
date3=$(date +%Y%m%d)                      # 今天的日期
file1=${filename}${date1}                  # 底下三行在設定檔名
file2=${filename}${date2}
file3=${filename}${date3}

# 4. 將檔名建立吧!
touch "$file1"                             # 底下三行在建立檔案
touch "$file2"
touch "$file3"


上面的範例鳥哥使用了很多在十一章介紹過的概念: 包括小指令『 $(command) 』的取得訊息、變數的設定功能、變數的累加以及利用 touch 指令輔助! 如果你開始執行這個 sh03.sh 之後,你可以進行兩次執行:一次直接按 [Enter] 來查閱檔名是啥? 一次可以輸入一些字元,這樣可以判斷你的腳本是否設計正確喔!



原文出處:鳥哥的 Linux 私房菜 -- 學習 Shell Scripts
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[分享]HowTo Format Date For Display or Use In a Shell Script

HowTo Format Date For Display or Use In a Shell Script

by Vivek Gite on February 27, 2007 last updatedJanuary 15, 2014

How do I format date to display on screen on for my shell scripts as per my requirements on Linux or Unix like operating systems?

You need to use the standard date command to format date or time. You can use the same command with the shell script.

Syntax

The syntax is

date +FORMAT

OR

date +"%FORMAT"

OR

date +"%FORMAT%FORMAT"

OR

date +"%FORMAT-%FORMAT"

Task: Display date in mm-dd-yy format

Open a terminal and type the following date command:
$ date +"%m-%d-%y"
Sample output:

02-27-07

To turn on 4 digit year display:
$ date +"%m-%d-%Y"
Just display date as mm/dd/yy format:
$ date +"%D"

Task: Display time only

Type the following command:
$ date +"%T"
Outputs:

19:55:04

To display locale's 12-hour clock time, enter:
$ date +"%r"
Outputs:

07:56:05 PM

To display time in HH:MM format, type:
$ date +"%H-%M"
Sample outputs:

00-50

How do I save time/date format to the shell variable?


Simply type the following command at the shell prompt:
$ NOW=$(date +"%m-%d-%Y")
To display a variable use echo / printf command:
$ echo $NOW

A sample shell script


#!/bin/bash
NOW=$(date +"%m-%d-%Y")
FILE="backup.$NOW.tar.gz"
echo "Backing up data to /nas42/backup.$NOW.tar.gz file, please wait..."
# rest of script
# tar xcvf /nas42/backup.$NOW.tar.gz /home/ /etc/ /var
 

A complete list of FORMAT control characters supported by the date command


FORMAT controls the output. It can be the combination of any one of the following:




%FORMAT StringDescription
%%a literal %
%alocale's abbreviated weekday name (e.g., Sun)
%Alocale's full weekday name (e.g., Sunday)
%blocale's abbreviated month name (e.g., Jan)
%Blocale's full month name (e.g., January)
%clocale's date and time (e.g., Thu Mar 3 23:05:25 2005)
%Ccentury; like %Y, except omit last two digits (e.g., 21)
%dday of month (e.g, 01)
%Ddate; same as %m/%d/%y
%eday of month, space padded; same as %_d
%Ffull date; same as %Y-%m-%d
%glast two digits of year of ISO week number (see %G)
%Gyear of ISO week number (see %V); normally useful only with %V
%hsame as %b
%Hhour (00..23)
%Ihour (01..12)
%jday of year (001..366)
%khour ( 0..23)
%lhour ( 1..12)
%mmonth (01..12)
%Mminute (00..59)
%na newline
%Nnanoseconds (000000000..999999999)
%plocale's equivalent of either AM or PM; blank if not known
%Plike %p, but lower case
%rlocale's 12-hour clock time (e.g., 11:11:04 PM)
%R24-hour hour and minute; same as %H:%M
%sseconds since 1970-01-01 00:00:00 UTC
%Ssecond (00..60)
%ta tab
%Ttime; same as %H:%M:%S
%uday of week (1..7); 1 is Monday
%Uweek number of year, with Sunday as first day of week (00..53)
%VISO week number, with Monday as first day of week (01..53)
%wday of week (0..6); 0 is Sunday
%Wweek number of year, with Monday as first day of week (00..53)
%xlocale's date representation (e.g., 12/31/99)
%Xlocale's time representation (e.g., 23:13:48)
%ylast two digits of year (00..99)
%Yyear
%z+hhmm numeric timezone (e.g., -0400)
%:z+hh:mm numeric timezone (e.g., -04:00)
%::z+hh:mm:ss numeric time zone (e.g., -04:00:00)
%:::znumeric time zone with : to necessary precision (e.g., -04, +05:30)
%Zalphabetic time zone abbreviation (e.g., EDT)
See also:


原文出處:HowTo Format Date For Display or Use In a Shell Script
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|