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

📄 timedate.html

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 HTML
字号:
<!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.57"><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"><TABLEWIDTH="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">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 12. External Filters, Programs and Commands</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="textproc.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="TIMEDATE">12.3. Time / Date Commands</A></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 12-10. Using <BCLASS="COMMAND">date</B></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><ANAME="DATERANDREF"></A></P><P>The <BCLASS="COMMAND">date</B> command has quite a number of	       output 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>).	        <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><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:<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 <TTCLASS="USERINPUT"><B>&#62;&#62; newfile</B></TT> (for ordinary	      files).</P></TD></TR></TABLE></DIV></DD><DT><ANAME="ATREF"></A><BCLASS="COMMAND">at</B></DT><DD><P>The <BCLASS="COMMAND">at</B> job control command executes	      a given set of commands at a specified time. Superficially,	      it resembles <AHREF="system.html#CRONREF">cron</A>, however,	      <BCLASS="COMMAND">at</B> is chiefly useful for one-time execution	      of a command set.</P><P><TTCLASS="USERINPUT"><B>at 2pm January 15</B></TT> prompts for a set of	      commands to execute at that time. These commands should be	      shell-script compatible, since, for all practical	      purposes, the user is typing in an executable shell	      script a line at a time.	Input terminates with a <AHREF="special-chars.html#CTLDREF">Ctl-D</A>.</P><P>Using either the <TTCLASS="OPTION">-f</TT> option or input	      redirection (<SPANCLASS="TOKEN">&#60;</SPAN>), <BCLASS="COMMAND">at</B>	      reads a command list from a file. This file is an	      executable shell script, though it should, of course,	      be noninteractive. Particularly clever is including the	      <AHREF="extmisc.html#RUNPARTSREF">run-parts</A> command in	      the file to execute a different set of scripts.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>at 2:30 am Friday &#60; at-jobs.list</B></TT> <TTCLASS="COMPUTEROUTPUT">job 2 at 2000-10-27 02:30</TT> 	      </PRE></TD></TR></TABLE>	    </P></DD><DT><BCLASS="COMMAND">batch</B></DT><DD><P>The <BCLASS="COMMAND">batch</B> job control command is similar to	      <BCLASS="COMMAND">at</B>, but it runs a command list when the system	      load drops below <TTCLASS="LITERAL">.8</TT>. Like	      <BCLASS="COMMAND">at</B>, it can read commands from a file with the	      <TTCLASS="OPTION">-f</TT> option.</P></DD><DT><BCLASS="COMMAND">cal</B></DT><DD><P>Prints a neatly formatted monthly calendar to	      <TTCLASS="FILENAME">stdout</TT>. Will do current year or a large	      range of past and future years.</P></DD><DT><BCLASS="COMMAND">sleep</B></DT><DD><P>This is the shell equivalent of a wait loop. It pauses for a	      specified number of seconds, doing nothing. It can be useful	      for timing or in processes running in the background,	      checking for a specific event every so often (polling),	      as in <AHREF="debugging.html#ONLINE">Example 29-6</A>.	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;sleep 3     # Pauses 3 seconds.</PRE></TD></TR></TABLE>	    </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">sleep</B> command defaults to	      seconds, but minute, hours, or days may also be specified.	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;sleep 3 h   # Pauses 3 hours!</PRE></TD></TR></TABLE>            </P></TD></TR></TABLE></DIV><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 <AHREF="system.html#WATCHREF">watch</A> command may	      be a better choice than <BCLASS="COMMAND">sleep</B> for running	      commands at timed intervals.</P></TD></TR></TABLE></DIV></DD><DT><BCLASS="COMMAND">usleep</B></DT><DD><P><ICLASS="EMPHASIS">Microsleep</I> (the <SPANCLASS="QUOTE">"u"</SPAN>	      may be read as the Greek <SPANCLASS="QUOTE">"mu"</SPAN>, or micro-	      prefix). This is the same as <BCLASS="COMMAND">sleep</B>,	      above, but <SPANCLASS="QUOTE">"sleeps"</SPAN> in microsecond	      intervals. It can be used for fine-grain timing, or for	      polling an ongoing process at very frequent intervals.</P><P>  	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;usleep 30     # Pauses 30 microseconds.</PRE></TD></TR></TABLE>	    </P><P>This command is part of the Red Hat <ICLASS="EMPHASIS">initscripts /	      rc-scripts</I> package.</P><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The <BCLASS="COMMAND">usleep</B> command does not	      provide particularly accurate timing, and is therefore	      unsuitable for critical timing loops.</P></TD></TR></TABLE></DIV></DD><DT><BCLASS="COMMAND">hwclock</B>, <BCLASS="COMMAND">clock</B></DT><DD><P>The <BCLASS="COMMAND">hwclock</B> command accesses or	      adjusts the machine's hardware clock. Some	      options require root privileges. The	      <TTCLASS="FILENAME">/etc/rc.d/rc.sysinit</TT> startup file	      uses <BCLASS="COMMAND">hwclock</B> to set the system time	      from the hardware clock at bootup.</P><P>The <BCLASS="COMMAND">clock</B> command is a synonym for	      <BCLASS="COMMAND">hwclock</B>.</P></DD></DL></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="moreadv.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="textproc.html">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Complex Commands</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="external.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Text Processing Commands</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

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