shell中的時間值提取(date)

 

shell中的時間值提取(date)
web


方法1
shell

# date +%Fbash

wKiom1SMBuDwMay8AAAowa-C_Mg312.jpg

# date +%Tspa

wKiom1SMCATwYnFnAAAk16M7I0E279.jpg

 

# cat time.sh
#!/bin/bash

DATE=`date +%F | sed 's/-//g'``date +%T | sed 's/://g'`
echo $DATE
# chmod u+x time.sh
# sh time.sh
2014082709352

 

方法2code

「date +%Y%m%d%H%M%S」獲取時間信息串
blog

[we@h p]$ date +%Y%m%d%H%M%S;date
20160410021109
Sun Apr 10 02:11:09 CST 2016

 

方法3字符串

「date +%s」獲取絕對秒數(UTC),使用「-d」參數還原時間
get

#!/bin/bash

print_date()
{
  /bin/date
}

echo -n -e '\f$(date)\t\t| '
print_date

echo -n -e 'SEC=$(date +%s)\t| '
SEC=$(date +%s)
echo "$SEC"

echo -n -e '$(date -d @$SEC)| '
date -d @$SEC

[web@h p] sh date.sh
$(date)         | Tue Apr 12 22:25:41 CST 2016
SEC=$(date +%s) | 1460471141
$(date -d @$SEC)| Tue Apr 12 22:25:41 CST 2016

 

生成與時間相關的文件名稱
it


 應用:io

  例如用命令替換的方式生成帶有時間信息的文件名。

 1 $ touch ./reslog-"`date`".txt
 2 $ ll
 3 total 0
 4 -rw-r--r-- 1 root root 0 Sep 12 05:43 are
 5 -rw-r--r-- 1 root root 0 Sep 12 05:43 reslog-Mon Sep 12 05:43:25 CST 2016.txt
 6 -rw-r--r-- 1 root root 0 Sep 12 05:43 reslog-Mon Sep 12 05:43:36 CST 2016.txt
 7 -rw-r--r-- 1 root root 0 Sep 12 05:43 reslog-Mon Sep 12 05:43:37 CST 2016.txt
 8 -rw-r--r-- 1 root root 0 Sep 12 05:43 reslog-Mon Sep 12 05:43:38 CST 2016.txt
 9 $ touch ./reslog-"`date +%s`".txt
10 $ ll
11 total 0
12 -rw-r--r-- 1 root root 0 Sep 12 05:43 are
13 -rw-r--r-- 1 root root 0 Sep 12 05:50 reslog-1473630651.txt
14 -rw-r--r-- 1 root root 0 Sep 12 05:50 reslog-1473630652.txt
15 -rw-r--r-- 1 root root 0 Sep 12 05:50 reslog-1473630653.txt
16 -rw-r--r-- 1 root root 0 Sep 12 05:50 reslog-1473630654.txt
17 $ touch ./reslog-"`date +%Y%m%d%H%M%S`".txt
18 $ ll
19 total 0
20 -rw-r--r-- 1 root root 0 Sep 12 05:43 are
21 -rw-r--r-- 1 root root 0 Sep 12 05:53 reslog-20160912055308.txt
22 -rw-r--r-- 1 root root 0 Sep 12 05:53 reslog-20160912055309.txt
23 -rw-r--r-- 1 root root 0 Sep 12 05:53 reslog-20160912055310.txt

  * 「date +%H%M%S」,這裏的小時是「00~23」格式的,若是使用「date + %I」顯示就是「01~12」格式的小時。

 

  這個風格更加直觀

$ touch ./reslog-"`date +%F_%T`".txt
$ ll
total 0
-rw-r--r-- 1 root root 0 Sep 12 06:00 are
-rw-r--r-- 1 root root 0 Sep 12 06:02 reslog-2016-09-12_06:02:18.txt
-rw-r--r-- 1 root root 0 Sep 12 06:02 reslog-2016-09-12_06:02:19.txt
-rw-r--r-- 1 root root 0 Sep 12 06:02 reslog-2016-09-12_06:02:20.txt
$ touch ./reslog-"`date +%F\ %T`".txt
$ ll
total 0
-rw-r--r-- 1 root root 0 Sep 12 06:00 are
-rw-r--r-- 1 root root 0 Sep 12 06:06 reslog-2016-09-12 06:06:18.txt
-rw-r--r-- 1 root root 0 Sep 12 06:06 reslog-2016-09-12 06:06:19.txt
-rw-r--r-- 1 root root 0 Sep 12 06:06 reslog-2016-09-12 06:06:20.txt

 

時間設置


命令:

  date,打印、設定日期和時間

選項:

    -d, --date=STRING

      顯示時間;不是當前時間,是字符串指定的時間。

    -s, --set=STRING

      設置時間

        STRING:

            "Sun, 29 Feb 2004 16:21:42 -0800"

            "2014-02-29 16:21:42 -0800"

            "2024-02-29 16:21 -0800"

            "2034-02-29 -0800"

            "2044-02-29 16:21:42"

            "16:00 next Thursday"

            "next Thursday"

例子:

  設置時間

[root@hp430G2 ~]# date -s "2014-08-27 17:30:55"

  顯示時間

[weblogic@hp430G2 ~]$ date -d "20140312 17:22:21"
Wed Mar 12 17:22:21 CST 2014
[weblogic@hp430G2 ~]$ date -d "2014-03-12 17:22:21"
Wed Mar 12 17:22:21 CST 2014
相關文章
相關標籤/搜索