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

📄 347-349.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=347-349//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="345-347.html">Previous</A></TD>

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

<TD><A HREF="349-351.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<P><FONT SIZE="+1"><B>The PATH Variable</B></FONT></P>

<P>The <TT>PATH</TT> variable lists the directories in which the shell searches for commands. The shell searches those directories in the order they&#146;re listed. If <TT>PATH=/bin:/usr/bin/:.</TT>, whenever the shell interprets a command, it first looks in the directory /bin. If it can&#146;t find the command there, the shell looks in the directory /usr/bin. Finally, the shell searches the . directory (remember that the dot represents your current directory). When you enter <TT>cal</TT> to print this month&#146;s calendar, the shell first looks in /bin. Because the command isn&#146;t there, the shell then looks in /usr/bin and finds it.</P>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>TIP:&nbsp;&nbsp;</B>If you have a personalized command named <TT>cal</TT>, the shell never finds it; the shell executes the <TT>cal</TT> command in /usr/bin first whenever you give the command. Give your commands names that aren&#146;t the same as system commands.<HR></FONT>

</BLOCKQUOTE>

<P>You may want to put all your shell scripts in one directory and change the <TT>PATH</TT> variable to include that directory. This arrangement allows you to execute your shell scripts from whatever directory you happen to be in. To do this, follow these steps:</P>

<DL>

<DD><B>1.</B>&nbsp;&nbsp;Create a directory to hold the scripts. Use the <TT>mkdir $HOME/bin</TT> command to create the bin subdirectory in your home directory.

<DD><B>2.</B>&nbsp;&nbsp;Move each shell script to that subdirectory. For example, to move a shell script named <TT>stamp</TT> to your bin subdirectory, use the <TT>mv stamp $HOME/bin</TT> command.

<DD><B>3.</B>&nbsp;&nbsp;Add the script subdirectory to your <TT>PATH</TT> variable with the <TT>PATH=$PATH:$HOME/bin</TT> command. Do this in your .profile file so that the change takes effect every time you log in to your system.

</DL>

<P>You need to create that new bin directory and modify the <TT>PATH</TT> variable only once. Under Linux, the directory called /usr/local/bin is created to hold &#147;local&#148; commands and scripts that aren&#146;t part of the standard Linux package but that you&#146;ve added locally and have made available to all users. In this case, you should expect that /usr/local/bin is also part of <TT>PATH</TT>.</P>

<P><FONT SIZE="+1"><B>The MAIL Variable</B></FONT></P>

<P>The <TT>MAIL</TT> variable contains the name of the file that holds your e-mail. Whenever mail comes into the system for you, it&#146;s put into the file specified by the <TT>MAIL</TT> variable. If you have a program that notifies you when new mail has arrived, it checks the file associated with the <TT>MAIL</TT> variable.</P>

<P><FONT SIZE="+1"><B>The PS1 Variable</B></FONT></P>

<P>The <TT>PS1</TT> variable holds the string of characters you see as your primary prompt. The prompt is the string of characters the shell displays whenever it&#146;s ready to receive a command. You see how you can change this variable&#151;and any of the others&#151;in the section &#147;Customizing Linux Shells&#148; near the end of this chapter.</P>

<P><FONT SIZE="+1"><B>The TERM Variable</B></FONT></P>

<P>The <TT>TERM</TT> variable is used to identify your terminal type. Programs that operate in full-screen mode, such as the <TT>vi</TT> text editor, need this information.</P>

<P><FONT SIZE="+1"><B>The TZ Variable</B></FONT></P>

<P>The <TT>TZ</TT> variable holds a string that identifies your time zone. The <TT>date</TT> program and some other programs require this information.</P>

<P>Your computer system keeps track of time according to Greenwich Mean Time (GMT). If the <TT>TZ</TT> variable is set to <TT>PST8PDT</TT>, the time and date are determined as Pacific Standard Time (PST), eight hours west of GMT, with support for Pacific Daylight Savings Time (PDT). Your computer system automatically changes between daylight savings time and standard time.</P>

<P><FONT SIZE="+1"><B>The LOGNAME Variable</B></FONT></P>

<P>The <TT>LOGNAME</TT> variable holds your login name, the name or string of characters that the system associates you with. Among the things the <TT>LOGNAME</TT> variable is used for is to identify you as the owner of your files, as the originator of any processes or programs you may be running, and as the author of mail or messages sent by the <TT>write</TT> command.</P>

<P>The following example is an extension of <TT>safrm</TT>, a shell script created for the safe removal of files. The <TT>LOGNAME</TT> variable is used to remove all the files you own from the directory /tmp. To do that, the shell script uses the <TT>find</TT> command. The <TT>find</TT> command has a number of options; the shell script uses this <TT>find</TT> command line:</P>

<!-- CODE SNIP //-->

<PRE>

find /tmp -user $LOGNAME -exec rm &#123;&#125; \;

</PRE>

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

<P>The first parameter, <TT>/tmp</TT>, is the directory to search. The option <TT>-user</TT> indicates that you want to search for all files that belong to a specified user. Before the command is executed, the shell replaces <TT>$LOGNAME</TT> with the current user&#146;s login name. The option <TT>-exec</TT> indicates that the following command is to be applied to every file found by the <TT>find</TT> program. In this case, the <TT>rm</TT> program is used to remove the found files. The braces (&#123;&#125;) represent the position of each filename passed to the <TT>rm</TT> command. The last two characters, <TT>\</TT>;, are required by the <TT>find</TT> command (an example of using the backslash to pass a character on to a program without being interpreted by the shell). Add this command line to the shell script in Listing 18.1 to obtain a program that removes files safely and also cleans up anything a user has in the /tmp directory that&#146;s more than 10 days old.</P>

<P><B>Listing 18.1</B> The safrm Shell Script</P>

<!-- CODE //-->

<PRE>

# Name:     safrm

# Purpose:  copy files to directory /tmp, remove them

#             from the current directory, clean up /tmp,

#            and finally send mail to user

# first copy all parameters to /tmp

cp $* /tmp

# remove the files

rm $*

# create a file to hold the mail message

#    The file&#146;s name is set to msg

#    followed by process ID number of this process

#    For example, msg1208

msgfile=/tmp/msg$$

# construct mail message

date &gt; $msgfile

echo &#147;These files were deleted from /tmp&#148; &gt;&gt;$msgfile

# get list of files to be deleted from tmp

# -mtime &#43;10 gets all files that haven&#146;t been

# modified in 10 or more days, -print displays the names.

find /tmp -user $LOGNAME -mtime &#43;10 -print &gt;&gt; $msgfile

# remove the appropriate files from /tmp

find /tmp -user $LOGNAME -mtime &#43;10 -exec rm &#123;&#125; \;

# mail off the message

mail $LOGNAME &lt; $msgfile

# clean up

rm $msgfile

</PRE>

<!-- END CODE //-->

<P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="345-347.html">Previous</A></TD>

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

<TD><A HREF="349-351.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 + -