📄 ch51_10.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 51] 51.10 The date Command </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:57:29Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch51_01.htm"TITLE="51. Miscellaneous Useful Programs and Curiosities"><LINKREL="prev"HREF="ch51_09.htm"TITLE='51.9 Making a "Login" Shell'><LINKREL="next"HREF="ch51_11.htm"TITLE="51.11 Making an Arbitrary-Size File for Testing "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch51_09.htm"TITLE='51.9 Making a "Login" Shell'><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT='Previous: 51.9 Making a "Login" Shell'BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 51<BR>Miscellaneous Useful Programs and Curiosities</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch51_11.htm"TITLE="51.11 Making an Arbitrary-Size File for Testing "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 51.11 Making an Arbitrary-Size File for Testing "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7914">51.10 The date Command </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-60427"></A><ACLASS="indexterm"NAME="AUTOID-60429"></A><ACLASS="indexterm"NAME="AUTOID-60432"></A>All UNIX systems come with a program called <EMCLASS="emphasis">date</EM>.It shows you the time from the<SPANCLASS="link">system clock (<ACLASS="linkend"HREF="ch51_02.htm"TITLE="How UNIX Keeps Time">51.2</A>)</SPAN>.(So, the time is only as accurate as your system's clock.)For example:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>date</B></CODE>Fri Dec 13 08:06:39 PST 1996</PRE></BLOCKQUOTE></P><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="date">date</A><BR></TH><TDVALIGN="TOP">If you need parts of that date, you can split the line into pieces.There are examples in articles<ACLASS="xref"HREF="ch16_18.htm"TITLE="Listing Files You've Created/Edited Today ">16.18</A>,<ACLASS="xref"HREF="ch21_03.htm"TITLE="Unique Names for Temporary Files ">21.3</A>,and<ACLASS="xref"HREF="ch02_05.htm"TITLE="Tips for Speeding up Slow Logins ">2.5</A>.Most newer versions of <EMCLASS="emphasis">date</EM> (including the one on the Power Toolsdisc) will also let you set the format of the date.To do that, type a single argument that starts with a plus (<CODECLASS="literal">+</CODE>)and has format specification characters in it.Check your online manual page for a list of format specificationcharacters.The argument has to be all one word, so you'll usually need to put<SPANCLASS="link">quotes (<ACLASS="linkend"HREF="ch08_14.htm"TITLE="Bourne Shell Quoting ">8.14</A>)</SPAN>around it.Here's a simple example:</TD></TR></TABLE><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>date +'Today is %d %h 19%y.'</B></CODE>Today is 13 Dec 1996.</PRE></BLOCKQUOTE></P><PCLASS="para">You'll usually use this in a shell script or<SPANCLASS="link">alias (<ACLASS="linkend"HREF="ch10_02.htm"TITLE="Aliases for Common Commands ">10.2</A>)</SPAN>.Because <EMCLASS="emphasis">date</EM> can mix any other text with the dates it outputs,you can do some surprising and useful things.Greg Ubben uses it to construct arithmeticexpressions for performing calculations on dates.For example, since <CODECLASS="literal">date +%U</CODE> isn't available on all systems,the following computes the week number of the year(Sunday as the first day) and makes it available in the Bourne shellas <CODECLASS="literal">$weekno</CODE>:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch45_28.htm"TITLE="45.28 Quick Reference: expr ">expr</A> <ACLASS="co"HREF="ch45_31.htm"TITLE="45.31 Nested Command Substitution ">`...`</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">weekno=`expr \`date +"%j / 7 + ( ( %w + 6 ) %% 7 < %j %% 7 )"\``</PRE></TD></TR></TABLE></P><PCLASS="para">For some other "real-life" examples, see articles<ACLASS="xref"HREF="ch16_16.htm"TITLE="Picking a Unique Filename Automatically ">16.16</A>,<ACLASS="xref"HREF="ch18_03.htm"TITLE="Files with Two or More Names">18.3</A>,and<ACLASS="xref"HREF="ch21_14.htm"TITLE="Automatically Appending the Date to a Filename ">21.14</A>.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch51_09.htm"TITLE='51.9 Making a "Login" Shell'><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT='Previous: 51.9 Making a "Login" Shell'BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch51_11.htm"TITLE="51.11 Making an Arbitrary-Size File for Testing "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 51.11 Making an Arbitrary-Size File for Testing "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">51.9 Making a "Login" Shell</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">51.11 Making an Arbitrary-Size File for Testing </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed & awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -