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

📄 364-366.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=364-366//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

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

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

<TD><A HREF="367-369.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H4 ALIGN="LEFT"><A NAME="Heading24"></A><FONT COLOR="#000077">Aliasing Commands</FONT></H4>

<P>Command aliasing allows you to define a name for a command. Consider this example: The <TT>man</TT> command displays Linux documentation, or <BIG>man pages</BIG>. To make the word <TT>help</TT> an alias, or alternative, for <TT>man</TT>, enter the following:</P>

<!-- CODE SNIP //-->

<PRE>

alias help=man

</PRE>

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

<P>Now you can enter <TT>help cp</TT> or <TT>man cp</TT> to display Linux man pages about the <TT>cp</TT> command.</P>

<P>You also can use aliases with commands that have options or arguments. For example, if you want to list the names of all the files in the current directory sorted in descending order by the time they were last modified (so that the most recent files are at the bottom of the list), you can use this command:</P>

<!-- CODE SNIP //-->

<PRE>

ls -art

</PRE>

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

<P>The <TT>ls</TT> command is the command to list files; the <TT>-a</TT> option specifies all files, the <TT>-r</TT> option arranges the files in reverse, descending order, and the <TT>-t</TT> option sorts by time last modified. That&#146;s a lot to remember. You can assign the alias <TT>timedir</TT> to this complex command with the following command:</P>

<!-- CODE SNIP //-->

<PRE>

alias  timedir=&#147;ls  -art&#148;

</PRE>

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

<P>The quotation marks (&#147;&#148;) are necessary because the shell expects the alias for <TT>timedir</TT> to be terminated by a space or &lt;Return&gt;. Now, if you enter <TT>timedir</TT>, you get the directory listing you want.</P>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>NOTE:&nbsp;&nbsp;</B>Setting an alias from the command line keeps that alias in effect only for the current session. To have the alias active whenever you log in, include the alias definition in the .profile file if you use the Bourne shell; keep it in the .login file if you use the C shell.<HR></FONT>

</BLOCKQUOTE>

<H4 ALIGN="LEFT"><A NAME="Heading25"></A><FONT COLOR="#000077">Completing Commands</FONT></H4>

<P>Command completion allows you to type the beginning of a filename and then press the &lt;Tab&gt; key to expand the filename. This can save time and spelling mistakes when entering a command. If two files share a common prefix, Linux expands the command to the last common character, stops expanding the filename, and then beeps. You need to provide the unique filename.

</P>

<H4 ALIGN="LEFT"><A NAME="Heading26"></A><FONT COLOR="#000077">Adding Text with Cut and Paste</FONT></H4>

<P>Red Hat Linux and Slackware Linux both offer a program that can be started at boot time that allows you to use the mouse to select text from anywhere on-screen and then paste the text onto the command line for the shell to interpret. To get a mouse cursor, you simply press one of the mouse buttons. You then select the desired text from anywhere on-screen by first clicking with the left mouse button on the beginning of the text and, while holding down the button, dragging the cursor to the desired end point of the text. After you select the text, click with the right mouse button to copy the text to the command line.

</P>

<H3><A NAME="Heading27"></A><FONT COLOR="#000077">Working with Shell Scripts</FONT></H3>

<P>The shell accepts commands, interprets them, and arranges for the operating system to execute the commands in the manner you specify. In the previous sections, you saw how the shell interprets special characters to complete filenames, redirects input and output, connects processes through pipes, and puts jobs or processes in the background.

</P>

<P>You can type commands at the terminal, or they can come from a file. A <BIG>shell script</BIG> is a collection of one or more shell commands in a file. To execute the commands, you type the name of the file. The advantages to this approach include the following:</P>

<DL>

<DD><B>&#149;</B>&nbsp;&nbsp;You don&#146;t have to retype a sequence of commands.

<DD><B>&#149;</B>&nbsp;&nbsp;You determine the steps to accomplish a goal once.

<DD><B>&#149;</B>&nbsp;&nbsp;You simplify operations for yourself and others.

</DL>

<P>By using variables and keywords, you can write programs that the shell can interpret. This is useful because it allows you to create general shell scripts you or others can use in various situations.

</P>

<P>Suppose that after you log in, you regularly like to see who&#146;s logged in to your system, run a program named <TT>calendar</TT> that displays your appointments for today and tomorrow, and print the current date and time to the screen. To do all that, you enter the following commands:</P>

<!-- CODE SNIP //-->

<PRE>

who

calendar

date

</PRE>

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

<P>If you put these three commands into a file named whatsup and make that file executable, you have a shell script that you can execute just like any other command. The file whatsup must be a text file. You can use the <TT>vi</TT> or <TT>emacs</TT> text editor to put the commands in the whatsup file. To make the file executable, enter this command:</P>

<!-- CODE SNIP //-->

<PRE>

chmod &#43;x whatsup

</PRE>

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

<P>The <TT>chmod</TT> command modifies or sets the permissions for a file. The <TT>&#43;x</TT> option makes the file executable&#151;that is, it makes the file work just like a standard Linux command. Putting commands into the file and making the file executable are both one-time operations. From that point on, you can enter <TT>whatsup</TT> to execute your shell script. You can use the shell script just like any other command. For example, to print the results of the <TT>whatsup</TT> command, enter the following:</P>

<!-- CODE SNIP //-->

<PRE>

whatsup | lp

</PRE>

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

<P>To put the results of the <TT>whatsup</TT> command into a file named info for future reference, enter this:</P>

<!-- CODE SNIP //-->

<PRE>

whatsup &gt; info

</PRE>

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

<P>As a review, follow these steps to create a shell script that you can use whenever you want:

</P>

<DL>

<DD><B>1.</B>&nbsp;&nbsp;Use a text editor, such as <TT>vi</TT> or <TT>emacs</TT>, to put the shell commands into a text or ASCII file. In the preceding example, the commands were put in the file named whatsup.

<DD><B>2.</B>&nbsp;&nbsp;Make it so that you have execute permission on the file. Use <TT>chmod &#43;x filename</TT> (for example, <TT>chmod &#43;x whatsup</TT>).

<DD><B>3.</B>&nbsp;&nbsp;Test the command by typing the name of the command and pressing &lt;Return&gt;.

</DL>

<P>After using this process a few times, you&#146;ll see how easy it is to create useful scripts. Of course, the hardest part is figuring out which shell commands to use and how to use the shell&#146;s programming capabilities to express the steps you need to carry out.

</P>

<P>You can test a shell script and see all the steps it goes through by entering this command:</P>

<!-- CODE SNIP //-->

<PRE>

sh -x script-name

</PRE>

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

<P>In this syntax, <BIG>script-name</BIG> is the name of the file that holds the script you&#146;re considering. The <TT>sh -x</TT> command displays all the steps the script goes through and is useful when you&#146;re trying to debug a script.</P><P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

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

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

<TD><A HREF="367-369.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 + -