?? unx05.htm
字號:
<H3 ALIGN="CENTER">
<CENTER><A ID="I8" NAME="I8">
<FONT SIZE=4><B>Finding the Date and Time</B>
<BR></FONT></A></CENTER></H3>
<P>When used by an ordinary user, the date command does only one thing: it displays the date and the time. The system administrator can use date to set the date and the time.
<BR></P>
<P>Here's how you use date to find out the current date and time:
<BR></P>
<PRE>$ date
Sat Sep 28 1:45:58 EDT 1991</PRE>
<P>This is the simplest form of executing the date command, but not the most useful. This command has many options that let you extract any part of the usual output and display that part alone or with other parts.
<BR></P>
<P>First, look at the individual options. Unlike most UNIX commands, which usually have single letters as options, date's options are strings of characters. The option string begins with a plus sign (+) and each option is preceded with a percent sign (%).
You can include ordinary text in the option string, as you will see in the next example.
<BR></P>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="imp.gif" WIDTH = 68 HEIGHT = 35><B>TIP: </B>You should enclose the option string in either single or double quotation marks so that the shell won't interpret any characters in the ordinary text portion that it believes are special.
<BR></NOTE>
<HR ALIGN=CENTER>
<P>The a option outputs the abbreviated name of the day, and the A option provides the unabbreviated name:
<BR></P>
<PRE>$ date +%a
Sat
$ date +%A
Saturday</PRE>
<P>The b option outputs the abbreviated name of the month, and the B option provides the unabbreviated name:
<BR></P>
<PRE>$ date +%b
Sep
$ date +%B
September</PRE>
<P>The d option provides the day of the month in two-digit numeric form, and the e option outputs the day of the month with a space preceding the day for days 1 to 9. In the following example, the date is the 28th day of the month:
<BR></P>
<PRE>$ date +%d
28
$ date +%e
28</PRE>
<P>If you execute these two commands again early the next month—on the fourth, for example—you would see the following:
<BR></P>
<PRE>$ date +%d
4
$ date +%e
4</PRE>
<P>The D option outputs the common numerical date format (month/day/year) used in the United States:
<BR></P>
<PRE>$ date +%D
09/28/91</PRE>
<P>Other options—c, x, and X—output the date in the format for whichever country was specified when SVR4 was installed. If you designated a country other than the United States during installation, try these options on your own; their output
differs for different countries.
<BR></P>
<P>The options H and I output the hour in numeric form: H in 24 hour or military form, and I in 12 hour form.
<BR></P>
<PRE>$ date +%H
13
$ date +%I
1</PRE>
<P>The j option is rather interesting. It outputs the so-called "Julian" date—the day as one of the 365 days of the year (or 366 in a leap year). The following example shows the j option returning the date for September 28:
<BR></P>
<PRE>$ date +%j
271</PRE>
<P>This option is useful when calculating the elapsed time between two dates.
<BR></P>
<P>The U and W options both output the week as one of the 52 weeks of the year (or 53 in a leap year). They differ in that U begins the week with Sunday and W begins the week with Monday. When executed on September 29, 1991, date produces the following:
<BR></P>
<PRE>$ date +%U
39
$ date +%W
39</PRE>
<P>The m option outputs the month as one of the 12 months of the year:
<BR></P>
<PRE>$ date +%m
09</PRE>
<P>The M option gives the minutes value in the range of 00 to 59, and the S option shows the seconds value in the range of 00 to 61 (to allow for a "leap second" or two):
<BR></P>
<PRE>$ date +%M
48
$ date +%S
41</PRE>
<P>The R option combines the H and M options, and the T option combines H, M, and S:
<BR></P>
<PRE>$ date +%R
13:48
$ date +%T
13:48:51</PRE>
<P>The p option outputs either AM or PM, and r combines I, M, S, and p:
<BR></P>
<PRE>$ date +%p
AM
$ date +%r
1:48:25 AM</PRE>
<P>The w option shows the day of the week as a number between 0 and 6, with 0 representing Sunday:
<BR></P>
<PRE>$ date +%w
6</PRE>
<P>The y option shows the year as a number between 00 and 99, and the Y option shows the year as a four-digit value:
<BR></P>
<PRE>$ date +%y
91
$ date +%Y
1991</PRE>
<P>The Z option outputs the abbreviated time zone name for your computer. In the following example, the computer is located in the Eastern time zone, but during Daylight Savings Time:
<BR></P>
<PRE>$ date +%Z
EDT</PRE>
<P>You can combine two or more options along with text strings to produce more descriptive outputs, such as the following:
<BR></P>
<PRE>$ date "+Today is %A, the %e of %B, %Y"
Today is Saturday, the 28 of September, 1991</PRE>
<H3 ALIGN="CENTER">
<CENTER><A ID="I9" NAME="I9">
<FONT SIZE=4><B>Displaying a Monthly or Yearly Calendar with </B><B><I>cal</I></B>
<BR></FONT></A></CENTER></H3>
<P>The cal command is very simple but quite handy when you need to see the calendar of any month (or all 12) in any given year. Used with no arguments, cal simply prints a calendar of the current month:
<BR></P>
<PRE>$ cal
February 1994
S M Tu W Th F S
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28
$</PRE>
<P>If you specify a year, as in cal 1993, you get a calendar for all 12 months, but if you specify a month and a year, as in cal 4 1992, you get just that month.
<BR></P>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="caution.gif" WIDTH = 37 HEIGHT = 35><B>CAUTION: </B>If you specify a year as a two-digit number, cal gives you the calendar for that year. In other words, cal 93 produces a calendar for the year 93, not the year 1993.
<BR></NOTE>
<HR ALIGN=CENTER>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>Historical Note: </B> In 1752 the calendar was adjusted to account for the "discovery" of leap year by eliminating 11 days (September 3 through September 13). Type cal 9 1752 to see the strangest
month of all!
<BR></NOTE>
<HR ALIGN=CENTER>
<H3 ALIGN="CENTER">
<CENTER><A ID="I10" NAME="I10">
<FONT SIZE=4><B>Getting Information About Users</B>
<BR></FONT></A></CENTER></H3>
<P>To get information about users (including yourself), you can use several commands. The who command reports on users who are presently logged in, and finger reports on anyone who has an account on the computer. The id command reports information about
the user who invokes it.
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I11" NAME="I11">
<FONT SIZE=3><B>The </B><B><I>who</I></B><B> Command</B>
<BR></FONT></A></CENTER></H4>
<P>The who command normally reports certain information about logged-in users. By using its options, you can specify that it report information about the processes initiated by init, and that it report reboots, changes to the system clock, and logoffs.
(See chapters 18 and 35 for more information on processes and init.) If you invoke who with no options or arguments, you get the following output:
<BR></P>
<PRE>$ who
juucp tty00 Sep 28 11:13
pjh slan05 Sep 28 12:08</PRE>
<P>The output shows that two users are currently logged in: the user juucp, who logged in at 11:13, and the user pjh, who logged in at 12:08. Notice that juucp is logged in on a tty line (actually, juucp is a neighboring site that is called in over a
modem) and that pjh logged in over a network (STARLAN, which is shortened to slan in who's output).
<BR></P>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> The term tty is short for teletypewriter, the first kind of terminal to be connected to a UNIX computer system. Even though terminals have advanced significantly, the terminology has not changed. A
"neighboring site" is a computer that is located nearby.
<BR></NOTE>
<HR ALIGN=CENTER>
<P>The -u option adds the "time since the last activity" (also called the idle time) and the process ID number for each logged-in user. A "process ID number" or PID is an interger number assigned by UNIX to uniquely identify a given
process (usually, a process is a program that is running). PIDs are needed in UNIX because they allow—yea, encourage—simultaneous running of multiple processes. (See Part IV, "Process Control," for more information.)
<BR></P>
<PRE>$ who -u
juucp tty00 Sep 28 11:13 . 5890
pjh slan05 Sep 28 12:08 . 7354</PRE>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> If the user has been active within the last minute, who displays a dot (.) for the idle time.
<BR></NOTE>
<HR ALIGN=CENTER>
<P>The -T option reports a plus sign (+) if you are able to send messages to the user's terminal, or a minus sign (-) if it is not:
<BR></P>
<PRE>$ who -T
juucp + tty00 Sep 28 11:13
pjh + slan05 Sep 28 12:08</PRE>
<P>The -q (quick) option simply shows login IDs and a count of the logged-in users:
<BR></P>
<PRE>$ who -q
juucp pjh
# users=2</PRE>
<P>There's a special case for who: who am i. This case is useful if you are logged in from several terminals under different accounts, and you forget which one you are currently using:
<BR></P>
<PRE>$ who am i
pjh slan05 Sep 28 12:08</PRE>
<P><B>The </B><B><I>finger</I></B><B> Command</B>
<BR></P>
<P>You can use the finger command with or without arguments. Without arguments, finger's output looks a little like who's:
<BR></P>
<PRE>$ finger
Login Name TTY Idle When Where
pjh Pete Holsberg pts000 6d Mon 13:03
ajh Alan Holsberg sxt/002 Sat 15:00</PRE>
<P>This output lists all currently logged users—with a heading line—plus the user's full name and the name of the remote computer (in the "Where" column) if the person is logging in over a network.
<BR></P>
<P>This command is more useful if you give it a person's login ID as an argument, as in the following example:
<BR></P>
<PRE>$ finger lam
Login name: lam In real life: Pak Lam dp168
Directory: /home/stu/lam Shell: /usr/bin/ksh
On since Feb 23 19:07:31 on pts016 from pc2
2 days 21 hours Idle Time
No unread mail
No Plan.</PRE>
<P>Here, finger displays personal information for the user lam: his real name, home directory, which shell he uses, and when he last logged in and from which computer (pc2). The last line indicates that he does not have a text file called .plan in his home
directory. The finger command displays the contents of .plan and .project if they exist. As you can see, users can reveal as much or as little of themselves as they choose.
<BR></P>
<P>If your computer is on a network and you know another person's e-mail address, you can display information about that person by issuing a command such as the following:
<BR></P>
<PRE>$ finger holsberg@pilot.njin.net</PRE>
<P>The finger command provides many options that can suppress one or more fields of the normal, long output. However, because finger responds so quickly, simply disregarding that extra information when it appears onscreen is easier than learning the
appropriate option.
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I12" NAME="I12">
<FONT SIZE=3><B>The </B><B><I>id</I></B><B> Command</B>
<BR></FONT></A></CENTER></H4>
<P>The id command reports four things: the user ID number, login name, group ID number, and group name of the person who invokes it. If the real and effective IDs (see Chapter 44, "System Security") are not the same, id prints both sets of
values.
<BR></P>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> id is often used in shell scripts to restart the use of a shell by a certain user or group of users.
<BR>
<BR>When a system administrator creates an account for a user, that user is given a login ID and also placed in a group. This is important because UNIX provides access to files according to whether the user is the owner of the file and whether the user
belongs to the group that has been granted access to the file.
<BR></NOTE>
<HR ALIGN=CENTER>
<P>In the following example, the user ID is 102, the login name is pjh, and the user belongs to the root group, which is group number 0:
<BR></P>
<PRE>$ id
uid=102(pjh) gid=0(root)</PRE>
<H3 ALIGN="CENTER">
<CENTER><A ID="I13" NAME="I13">
<FONT SIZE=4><B>Switching Accounts with </B><B><I>su</I></B>
<BR></FONT></A></CENTER></H3>
<P>If you have more than one account on a system, you need not log out of one and then log into the second to use the second account. Instead, you can simply use the su (switch user) command. The system administrator uses su frequently to check a user's
complaint, because su enables the administrator to become that user and run the programs that were causing problems.
<BR></P>
<P>The usual su command syntax is
<BR></P>
<PRE>su - <I>userID</I></PRE>
<P>The minus sign tells su to cause the current user to take on the identity, <I>userID</I>. To do this, su executes the user's shell—as specified in the shell field of /etc/passwd—and invokes /etc/profile and that user's .profile file. The su
command then displays the user's normal environment. If the user omits the minus sign, su invokes the user's shell, but the environment is the same as before. Of course, before you can switch to another user, you must know that user's password—unless
you are the system administrator, in which case your privileges enable you to assume identities without using passwords.
<BR></P>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> /etc/profile and .profile are files that control the working environment of each user.
<BR></NOTE>
<HR ALIGN=CENTER>
<P>Suppose, the system administrator wishes to become user lam:
<BR></P>
<PRE>#su - lam
$</PRE>
<P>To reassume identity as sysadm, he merely logs out of lam's account.
<BR></P>
<H3 ALIGN="CENTER">
<CENTER><A ID="I14" NAME="I14">
<FONT SIZE=4><B>Learnig More About Commands with </B><B><I>man</I></B>
<BR></FONT></A></CENTER></H3>
<P>UNIX supplies information about its commands in two forms: reference manuals (printed documentation called man pages) and online files. To use the man pages, you simply find the appropriate reference manual and look up the command. However, reference
manuals seem to have lives of their own and are never to be found where you last put them. That's when online man pages—and the man command—become useful.
<BR></P>
<P>The simplest form of the man command takes a single argument: the name of the command in which you are interested. For example, here's the man page for man:
<BR></P>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -