⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timedate.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Time / Date Commands</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="External Filters, Programs and Commands"HREF="external.html"><LINKREL="PREVIOUS"TITLE="Complex Commands"HREF="moreadv.html"><LINKREL="NEXT"TITLE="Text Processing Commands"HREF="textproc.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="moreadv.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 15. External Filters, Programs and Commands</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="textproc.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="TIMEDATE"></A>15.3. Time / Date Commands</H1><DIVCLASS="VARIABLELIST"><P><B><ANAME="TDLISTING1"></A>Time/date and timing</B></P><DL><DT><ANAME="DATEREF"></A><BCLASS="COMMAND">date</B></DT><DD><P>Simply invoked, <BCLASS="COMMAND">date</B> prints the date and	      time to <TTCLASS="FILENAME">stdout</TT>. Where this command gets	      interesting is in its formatting and parsing options.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX51"></A><P><B>Example 15-10. Using <ICLASS="FIRSTTERM">date</I></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# Exercising the 'date' command   3&nbsp;   4&nbsp;echo "The number of days since the year's beginning is `date +%j`."   5&nbsp;# Needs a leading '+' to invoke formatting.   6&nbsp;# %j gives day of year.   7&nbsp;   8&nbsp;echo "The number of seconds elapsed since 01/01/1970 is `date +%s`."   9&nbsp;#  %s yields number of seconds since "UNIX epoch" began,  10&nbsp;#+ but how is this useful?  11&nbsp;  12&nbsp;prefix=temp  13&nbsp;suffix=$(date +%s)  # The "+%s" option to 'date' is GNU-specific.  14&nbsp;filename=$prefix.$suffix  15&nbsp;echo $filename  16&nbsp;#  It's great for creating "unique" temp filenames,  17&nbsp;#+ even better than using $$.  18&nbsp;  19&nbsp;# Read the 'date' man page for more formatting options.  20&nbsp;  21&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P>The <TTCLASS="OPTION">-u</TT> option gives the UTC (Universal	      Coordinated Time).</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>date</B></TT> <TTCLASS="COMPUTEROUTPUT">Fri Mar 29 21:07:39 MST 2002</TT>    <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>date -u</B></TT> <TTCLASS="COMPUTEROUTPUT">Sat Mar 30 04:07:42 UTC 2002</TT> 	      </PRE></TD></TR></TABLE>	      </P><P>This option facilitates calculating the time between	      different dates.</P><DIVCLASS="EXAMPLE"><HR><ANAME="DATECALC"></A><P><B>Example 15-11. <ICLASS="FIRSTTERM">Date</I> calculations</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# date-calc.sh   3&nbsp;# Author: Nathan Coulter   4&nbsp;# Used in ABS Guide with permission (thanks!).   5&nbsp;   6&nbsp;MPHR=60    # Minutes per hour.   7&nbsp;HPD=24     # Hours per day.   8&nbsp;   9&nbsp;diff () {  10&nbsp;        printf '%s' $(( $(date -u -d"$TARGET" +%s) -  11&nbsp;                        $(date -u -d"$CURRENT" +%s)))  12&nbsp;#                       %d = day of month.  13&nbsp;}  14&nbsp;  15&nbsp;  16&nbsp;CURRENT=$(date -u -d '2007-09-01 17:30:24' '+%F %T.%N %Z')  17&nbsp;TARGET=$(date -u -d'2007-12-25 12:30:00' '+%F %T.%N %Z')  18&nbsp;# %F = full date, %T = %H:%M:%S, %N = nanoseconds, %Z = time zone.  19&nbsp;  20&nbsp;printf '\nIn 2007, %s ' \  21&nbsp;       "$(date -d"$CURRENT +  22&nbsp;        $(( $(diff) /$MPHR /$MPHR /$HPD / 2 )) days" '+%d %B')"   23&nbsp;#       %B = name of month                ^ halfway  24&nbsp;printf 'was halfway between %s ' "$(date -d"$CURRENT" '+%d %B')"  25&nbsp;printf 'and %s\n' "$(date -d"$TARGET" '+%d %B')"  26&nbsp;  27&nbsp;printf '\nOn %s at %s, there were\n' \  28&nbsp;        $(date -u -d"$CURRENT" +%F) $(date -u -d"$CURRENT" +%T)  29&nbsp;DAYS=$(( $(diff) / $MPHR / $MPHR / $HPD ))  30&nbsp;CURRENT=$(date -d"$CURRENT +$DAYS days" '+%F %T.%N %Z')  31&nbsp;HOURS=$(( $(diff) / $MPHR / $MPHR ))  32&nbsp;CURRENT=$(date -d"$CURRENT +$HOURS hours" '+%F %T.%N %Z')  33&nbsp;MINUTES=$(( $(diff) / $MPHR ))  34&nbsp;CURRENT=$(date -d"$CURRENT +$MINUTES minutes" '+%F %T.%N %Z')  35&nbsp;printf '%s days, %s hours, ' "$DAYS" "$HOURS"  36&nbsp;printf '%s minutes, and %s seconds ' "$MINUTES" "$(diff)"  37&nbsp;printf 'until Christmas Dinner!\n\n'  38&nbsp;  39&nbsp;#  Exercise:  40&nbsp;#  --------  41&nbsp;#  Rewrite the diff () function to accept passed parameters,  42&nbsp;#+ rather than using global variables.</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="DATERANDREF"></A></P><P>The <ICLASS="FIRSTTERM">date</I> command has quite a		number of <ICLASS="FIRSTTERM">output</I> options. For		example <TTCLASS="OPTION">%N</TT> gives the nanosecond portion		of the current time. One interesting use for this is to		generate six-digit random integers.	       <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;date +%N | sed -e 's/000$//' -e 's/^0//'   2&nbsp;           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^   3&nbsp;# Strip off leading and trailing zeroes, if present.</PRE></TD></TR></TABLE>	      </P><P>There are many more options (try <BCLASS="COMMAND">man	        date</B>).</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;date +%j   2&nbsp;# Echoes day of the year (days elapsed since January 1).   3&nbsp;   4&nbsp;date +%k%M   5&nbsp;# Echoes hour and minute in 24-hour format, as a single digit string.   6&nbsp;   7&nbsp;   8&nbsp;   9&nbsp;# The 'TZ' parameter permits overriding the default time zone.  10&nbsp;date                 # Mon Mar 28 21:42:16 MST 2005  11&nbsp;TZ=EST date          # Mon Mar 28 23:42:16 EST 2005  12&nbsp;# Thanks, Frank Kannemann and Pete Sjoberg, for the tip.  13&nbsp;  14&nbsp;  15&nbsp;SixDaysAgo=$(date --date='6 days ago')  16&nbsp;OneMonthAgo=$(date --date='1 month ago')  # Four weeks back (not a month).  17&nbsp;OneYearAgo=$(date --date='1 year ago')</PRE></TD></TR></TABLE></P><P>See also <AHREF="special-chars.html#EX58">Example 3-4</A>.</P></DD><DT><ANAME="ZDUMPREF"></A><BCLASS="COMMAND">zdump</B></DT><DD><P>Time zone dump: echoes the time in a specified time zone.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>zdump EST</B></TT> <TTCLASS="COMPUTEROUTPUT">EST  Tue Sep 18 22:09:22 2001 EST</TT> 	      </PRE></TD></TR></TABLE>	      </P></DD><DT><ANAME="TIMREF"></A><BCLASS="COMMAND">time</B></DT><DD><P>Outputs very verbose timing statistics for executing a command.</P><P><TTCLASS="USERINPUT"><B>time ls -l /</B></TT> gives something	    like this:</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="COMPUTEROUTPUT">0.00user 0.01system 0:00.05elapsed 16%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+0outputs (149major+27minor)pagefaults 0swaps</TT></PRE></TD></TR></TABLE>	  </P><P>See also the very similar <AHREF="internal.html#TIMESREF">times</A> command in the previous	    section.</P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>As of <AHREF="bash2.html#BASH2REF">version 2.0</A>	    of Bash, <BCLASS="COMMAND">time</B> became a shell reserved word,	    with slightly altered behavior in a pipeline.</P></TD></TR></TABLE></DIV></DD><DT><ANAME="TOUCHREF"></A><BCLASS="COMMAND">touch</B></DT><DD><P>Utility for updating access/modification times of a	      file to current system time or other specified time,	      but also useful for creating a new file. The command	      <TTCLASS="USERINPUT"><B>touch zzz</B></TT> will create a new file	      of zero length, named <TTCLASS="FILENAME">zzz</TT>, assuming	      that <TTCLASS="FILENAME">zzz</TT> did not previously exist.	      Time-stamping empty files in this way is useful for	      storing date information, for example in keeping track of	      modification times on a project.	      </P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The <BCLASS="COMMAND">touch</B> command is	      equivalent to <TTCLASS="USERINPUT"><B>: &#62;&#62; newfile</B></TT>	      or <TT

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -