1. 程式人生 > >【Shell】獲取設定日期和延時

【Shell】獲取設定日期和延時

1.讀取日期

Linux:~ # date
Mon Dec 17 03:16:53 EST 2018

2.格式日期並列印

Linux:~ # date "+%d %B %Y"
17 December 2018

3.設定日期時間

Linux:~ # date -s "16 Dec 2018 5:20:21"
Sun Dec 16 05:20:21 EST 2018

上述命令為設定系統日期為:2018年12月16日 5點20分21秒

4.檢視命令執行時間

#!/bin/bash
start=$(date +%s)
echo "hellwo world"
sleep 1s
echo "after 1s"
end=$(
date
+%s) difference=$((end - start)) echo Time Taken to execute commands is $difference seconds.

執行結果

image

5.生成延時50秒的指令碼


  1 #!bin/bash
  2 echo Count:
  3 tput sc #儲存游標位置
  4 
  5 #迴圈50s
  6 for count in `seq 0 50` #迴圈生成數字
  7 do
  8   tput rc #恢復之前儲存的游標位置
  9   tput ed  #清除從當前游標位置到行尾之間的所有內容
 10
echo -n $count 11 sleep 1 12 done
View Code
日期內容 格式
星期

%a(例如:Sat)

%A(例如:Saturday)

%b(例如:Nov)

%B(例如:November)

%d(例如:31)
固定日期格式(mm/dd/yy) %D(例如:10/18/10)

%y(例如:18)

%Y(例如:2018)

小時 %I或%H(例如:08)
分鐘 %M(例如:23)
%S(例如:29)
納秒 %N(例如:097094592)