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

📄 362-364.html

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTML
字号:
<HTML>

<HEAD>

<TITLE>Special Edition Using Linux, Fourth Edition:Understanding Linux Shells</TITLE>

<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>

 -->




<!--ISBN=0789717468//-->

<!--TITLE=Special Edition Using Linux, Fourth Edition//-->

<!--AUTHOR=Jack Tackett//-->

<!--AUTHOR=Jr.//-->

<!--AUTHOR=Steve Burnett//-->

<!--PUBLISHER=Macmillan Computer Publishing//-->

<!--IMPRINT=Que//-->

<!--CHAPTER=18//-->

<!--PAGES=362-364//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="360-362.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="364-366.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<P><FONT SIZE="+1"><B>The at Command</B></FONT></P>

<P>The <TT>at</TT> command expects a time or date as a parameter and takes any number of command strings from its standard input. When the <TT>at</TT> command detects an end-of-file marker, it creates a Bourne shell script for execution at the time you specified.</P>

<P>The <TT>at</TT> command is flexible about the types of dates and times it accepts. For example, if you enter the command <TT>at now &#43; 1 day</TT>, the next commands, taken from the standard input, are executed tomorrow at this time. One way to use the <TT>at</TT> command is from within a shell script.</P>

<P>A <BIG>shell script</BIG> is nothing more than a file containing all the commands necessary to perform a series of commands. The name of the file then becomes your own addition to the Linux command language. One way of using the <TT>at</TT> command is shown here:</P>

<!-- CODE SNIP //-->

<PRE>

at now &#43; 1 day

command-1

command-2

</PRE>

<!-- END CODE SNIP //-->

<P>When placed in a shell script, these lines let you conveniently run one or more commands the next day. To run any number of different commands, simply enter new commands after the <TT>at</TT> command line. You can run any number of commands from this script.</P>

<P><FONT SIZE="+1"><B>The batch Command</B></FONT></P>

<P>The <TT>batch</TT> command is the logical equivalent of <TT>at now</TT>. If you attempt to use the <TT>at now</TT> command, you see an error message that says something along the lines of <TT>now has passed</TT>. The <TT>batch</TT> command works exactly as <TT>at now</TT> works if it were logically possible, with one minor exception: The <TT>cron</TT> daemon maintains a separate queue for commands generated by <TT>at</TT>, <TT>batch</TT>, and <TT>cron</TT>. Suppose that you entered the following commands into the file named backup:</P>

<!-- CODE SNIP //-->

<PRE>

tar  -cvf  tackettbkup  /usr/home/tackett

</PRE>

<!-- END CODE SNIP //-->

<P>Then you can tell the system to back up the directory /usr/home/tackett by using this command

</P>

<!-- CODE SNIP //-->

<PRE>

batch backup

</PRE>

<!-- END CODE SNIP //-->

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR>&#149; <B>See</B> &#147;Creating Your First <TT>vi</TT> File,&#148; <B>p. 182</B><HR></FONT>

</BLOCKQUOTE>

<P><FONT SIZE="+1"><B>The crontab Command</B></FONT></P>

<P>One of the best uses of the <TT>cron</TT> daemon is in automating the maintenance of a system. With <TT>cron</TT>, you as the system administrator can set up automatic backups of your system every morning at 4 a.m., Monday through Saturday. You install, delete, and list commands you want run in this fashion with the <TT>crontab</TT> command.</P>

<P>To run commands periodically, you must create a file in the <TT>crontab</TT> format. The crontab file consists of six fields separated by spaces or tabs. The first five fields are integers specifying minute (00&#150;59), hour (00&#150;23), day of the month (01&#150;31), month of the year (01&#150;12), and day of the week (0&#150;6, with 0 referring to Sunday). The sixth field is a command string. Each numeric field can contain an inclusive range of numbers (such as 1&#150;5 to indicate Monday through Friday) or discrete sets of numbers (such as 0,20,40 to indicate that an instruction should be run every 20 minutes). A field can also contain an asterisk to indicate all legal values.</P>

<P>The following example runs the <TT>calendar</TT> command every 20 minutes, starting at midnight Monday and ending at 11:40 p.m. Friday:</P>

<!-- CODE SNIP //-->

<PRE>

0,20,40 * * * 1-5 calendar -

</PRE>

<!-- END CODE SNIP //-->

<P>If you name this file cronfile, you can install it in the <TT>cron</TT> system by issuing the command <TT>crontab cronfile</TT>.</P>

<P>The <TT>cron</TT> daemon has a time granularity of one minute&#151;meaning, the shortest time duration you can work with is one minute. You, as system administrator, can place limits on the number of commands allowed to be run at any one time. Just because you ask <TT>cron</TT> to run an <TT>at</TT>, <TT>batch</TT>, or crontab file doesn&#146;t mean that it runs at precisely the time you&#146;ve indicated.</P>

<H3><A NAME="Heading20"></A><FONT COLOR="#000077">Understanding Command Feedback</FONT></H3>

<P>Linux provides instant feedback for commands that abort for one reason or another. In most cases, errors are limited to misspellings of the command name or badly formed filenames. If you attempt to run a nonexistent command, Linux replies with

</P>

<!-- CODE SNIP //-->

<PRE>

command: command not found

</PRE>

<!-- END CODE SNIP //-->

<P>If you try to use a nonexistent filename, Linux responds with

</P>

<!-- CODE SNIP //-->

<PRE>

command: file: No such file or directory

</PRE>

<!-- END CODE SNIP //-->

<P>If the error is caused by something other than a command-line error, the command itself usually reports what happened&#151;although not always in an easily decipherable form.

</P>

<P>If you try to run a command with <TT>nohup</TT> and haven&#146;t redirected the standard error, Linux automatically places any error messages in a file named nohup.out in the directory from which the command was run.</P>

<P>Because commands run by <TT>cron</TT> have less urgency, any errors&#151;indeed, any output placed on the standard output and not redirected&#151;is sent to you through e-mail.</P>

<H3><A NAME="Heading21"></A><FONT COLOR="#000077">Editing and Aliasing Shell Commands</FONT></H3>

<P>Different shells include features that provide shortcuts for running commands. <BIG>Command editing</BIG> lets you modify commands that have already been typed in. By using Linux&#146;s <BIG>command history</BIG> feature, you can recall commands you&#146;ve previously entered. <BIG>Aliasing</BIG> lets you create commands that represent other commands. <BIG>Command completion</BIG> lets you fill in the rest of a filename after you type part of it.</P>

<H4 ALIGN="LEFT"><A NAME="Heading22"></A><FONT COLOR="#000077">Editing Commands</FONT></H4>

<P>Command editing means that after you type a command&#151;and before you press &lt;Return&gt;&#151;you can edit or change parts of the command without having to retype most of it. To edit a command, press &lt;Esc&gt; to get into editing mode and then use any of the line-movement commands from the <TT>vi</TT> editor to modify the command. You can use &lt;Backspace&gt; to return to the portion of the command you want to change, and use other <TT>vi</TT> commands, such as <TT>x</TT> to delete a character, <TT>r</TT> to replace a character, and so on.</P>

<H4 ALIGN="LEFT"><A NAME="Heading23"></A><FONT COLOR="#000077">Viewing Command History</FONT></H4>

<P>The command history feature allows you to look back at previously entered commands and recall them. This feature saves you the time and trouble of retyping commands. When you combine this feature with command editing, you can easily correct mistakes in complicated commands and deal effectively with some repetitive tasks.

</P>

<P>In both shells, the <TT>history</TT> command displays the list of past commands the shell has saved. The commands are numbered. To execute command 10, for example, enter <TT>! 10</TT>. The <TT>bash</TT> shell also takes advantage of your PC&#146;s arrow keys; you can recall previous commands by pressing the &lt;&#8593;&gt; key.</P><P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="360-362.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="364-366.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





</td>
</tr>
</table>

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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