unx12.htm

来自「Unix Unleashed, Third Edition is written」· HTM 代码 · 共 1,677 行 · 第 1/5 页

HTM
1,677
字号

     <TR>

          <TD><BR></TD>

          <TD><BR>$ <B>echo ~/bin</B>

<BR>/usr/home/fran/bin

<BR>

<BR>$ <B>bindir=~/bin</B>

<BR>$ <B>echo $bindir</B>

<BR>~/bin</TD>

     </TR>

     <TR>

          <TD>~string</TD>

          <TD>A tilde followed by an alphanumeric string is replaced by the home directory of the named user. It is an error if no entry exists in the /etc/passwd file for string. For example,

</TD>

     </TR>

     <TR>

          <TD><BR></TD>

          <TD>$ <B>echo ~bill</B>

<BR>/usr/home/bill</TD>

     </TR>

     <TR>

          <TD>~+</TD>

          <TD> A tilde followed by a plus sign is replaced by the full pathname of the current directory. It is the same as writing $PWD or $PWD/.... For example,</TD>

     </TR>

     <TR>

          <TD><BR></TD>

          <TD>$ <B>pwd</B>

<BR>/usr/lib

<BR>$ <B>echo ~+/bin</B>

<BR>/usr/lib/bin

</TD>

     </TR>

     <TR>

          <TD>~- </TD>

          <TD>A tilde followed by a minus sign is replaced by the full pathname of the previous directory. It is the same as writing $OLDPWD or $OLDPWD/.... For example,</TD>

     </TR>

     <TR>

          <TD><BR></TD>

          <TD>$ <B>pwd</B>

<BR>/usr/lib

<BR>$ <B>cd ~/lib</B>

<BR>/usr/home/fran/lib

<BR>$ <B>echo ~-/bin</B>

<BR>/usr/lib/bin</TD>

     </TR>

     </TABLE>





<P>The tilde shorthand is a great time saver. The most common error people make when using it is that they forget that the tilde is recognized only at the beginning of a word, and that it can't be used in assignment expressions such as bindir=~/bin.

<BR></P>

<H5 ALIGN="CENTER">

<CENTER><A ID="I7" NAME="I7">

<FONT SIZE=3><B>Pattern Expressions</B>

<BR></FONT></A></CENTER></H5>

<P>A pattern expression is any word consisting of ordinary characters and one or more shell pattern-matching characters. The pattern-matching characters are the familiar *, ?, and [...] from the Bourne shell, as well as any of the following extended 
pattern-matching expressions:

<BR></P>

<TABLE BORDER>

<TR>

<TD>

<P>*(pattern[|pattern]...)</P>

<TD>

<P>Matches zero or more occurrences of the specified patterns. For example, time*(sheet|spent) matches the filenames time, timesheet, and timespent, but it doesn't match the filename timeused.</P>

<TR>

<TD>

<P>+(pattern[|pattern]...)</P>

<TD>

<P>Matches one or more occurrences of the specified patterns. For example, time+(.x|.y) matches time.x, time.x.x, and time.y, but it doesn't match time or time.x.y.</P>

<TR>

<TD>

<P>?(pattern[|pattern]...)</P>

<TD>

<P>Matches no or one occurrence of any of the patterns. For example, time?(.x|.y) matches time, time.x, and time.y, but it doesn't match time.x.x.</P>

<TR>

<TD>

<P>@(pattern[|pattern]...)</P>

<TD>

<P>Matches exactly one occurrence of the pattern. For example, time@(.x|.y) matches time.x or time.y, but it doesn't match either time or time.x.x.</P>

<TR>

<TD>

<P>!(pattern[|pattern]...)</P>

<TD>

<P>Same as * except that strings that would match the specified patterns are not considered matches. For example, time!(.x|.y) matches time, time.x.y, time.0, and everything beginning with time except for time.x and time.y.</P></TABLE>

<P>Note that the definition of pattern expressions is recursive. Each form contains one or more pattern strings. This means that nested pattern expressions are legal. Consider, for example, time*(.[cho]|.sh). It contains the pattern [cho] inside the 
pattern expression. The pattern time*(.*(sh|obj)) matches either of the filenames time.sh or time.obj.

<BR></P>

<P>The main value of these extended pattern-matching expressions is in enabling you to select a subset of files without having to list each filename explicitly on the command line. Pattern expressions are also legal in other contexts where the shell does 
pattern matching, such as in the expression of the case statement.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I8" NAME="I8">

<FONT SIZE=3><B>Command Substitution</B>

<BR></FONT></A></CENTER></H4>

<P>Another noteworthy enhancement provided by the Korn shell is a more convenient syntax for command substitutions. Remember from Chapter 11 on the Bourne shell that a string quoted with back-quotes ('command') is replaced with the standard output of 
command. The backquote notation isn't easy to use, though. The Korn shell supports the following alternate form in addition to the standard Bourne shell backquote notation:

<BR></P>

<PRE>$(command-list)</PRE>

<P>Not only does the parenthesized form avoid the problem of recognizing backquotes on printed listings, but it also acts as a form of quoting or bracketing. You can use all the standard quoting forms inside the parentheses without having to use 
backslashes to escape quotes. Furthermore, the parenthesized form nests. You can use $() expressions inside $() expressions without difficulty.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I9" NAME="I9">

<FONT SIZE=3><B>An Improved </B><B><I>cd</I></B><B> Command</B>

<BR></FONT></A></CENTER></H4>

<P>For directory movement, the Korn shell supports two new forms of the cd command:

<BR></P>

<PRE>

<BR>cd -



<BR>cd oldname newname</PRE>

<P>The command cd - is especially helpful. It switches back to the directory you were in before your previous cd command. This command makes it easy for you to switch to another directory temporarily, and then to move back to your working directory by 
typing cd -. The PWD and OLDPWD variables are maintained to carry the full pathnames of your current and previous directory, respectively. You can use these variables for writing commands to reference files in a directory without typing the full pathname.

<BR></P>

<P>You can use the cd oldname newname command to change a component of the pathname of your current directory. Thus, it makes lateral moves in a directory structure somewhat easier. For example, if your current directory is /usr/prod/bin and you want to 
switch to the directory /usr/test/bin, just type the command cd prod test. Similarly, the command cd usr jjv switches from /usr/prod/bin to /jjv/prod/bin, assuming that the latter directory exists.

<BR></P>

<H3 ALIGN="CENTER">

<CENTER><A ID="I10" NAME="I10">

<FONT SIZE=4><B>Aliases</B>

<BR></FONT></A></CENTER></H3>

<P>The command aliasing feature of the Korn shell is certainly one of its most attractive and flexible enhancements over the Bourne shell. It's an enhancement you'll start using right away.

<BR></P>

<P>When you define a command alias, you specify a shorthand term to represent a command string. When you type the shorthand term, it is replaced during command execution with the string that it represents. The command string can be more than just a command 

name. It can define stock options and arguments for the command as well.

<BR></P>

<P>For example, you might have one or more preferred ways of listing your directory contents. Personally, I like to use the -FC options on my ls command when I just want to see what's in the directory. Typing the command ls -FC ... repeatedly all day long, 

though, would not be one of my favorite things to do. The command alias feature makes it easy to set up a short hand for the ls command. You do it like this:

<BR></P>

<PRE>$ alias lx='ls -FC'</PRE>

<P>Now whenever you enter lx on the command line, the command ls -FC is executed.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I11" NAME="I11">

<FONT SIZE=3><B>Defining Aliases</B>

<BR></FONT></A></CENTER></H4>

<P>The alias command is a shell built-in, meaning that it is available to you only when running the Korn shell. It is not part of the UNIX operating system at large. You use the alias command to define new aliases and to list the command aliases currently 

in effect.

<BR></P>

<P>The general syntax of the alias command is

<BR></P>

<PRE>alias [ -tx ] [ name[=value] ... ]</PRE>

<P>The arguments of alias are one or more specifications, each beginning with an alias name. The alias name is the shorthand command that you enter at the terminal. Following an equal sign (=), you enter the text with which you want the shell to replace 
your shorthand. You should enclose the alias value string in single quotes to hide embedded blanks and special characters from immediate interpretation by the shell.

<BR></P>

<P>The Korn shell stores alias names and their definitions in an internal table kept in memory. Because it's not stored in a disk file, you lose your alias definitions whenever you log out or exit the Korn shell. To keep an alias from session to session, 
you need to define the alias in your login profile&#151;a file in your home directory named .profile). There's nothing tricky about it. The same command that you enter at the keyboard to define an alias works just as well when issued from a login profile 
script. Thus, for aliases you want to use over and over, simply type the alias command in your login profile; you only have to do it once. (For more information about using the login profile, see the section called &quot;Customizing&quot; near the end of 
this chapter.)

<BR></P>

<P>The syntax of the alias command enables you to define more than one alias on a command. The general syntax is

<BR></P>

<PRE>alias name = value [name = value]...</PRE>

<P>You don't usually write multiple definitions on one alias command. This is because you usually think them up one at a time. In your login profile, it's a good idea to write only one alias definition per alias command. This makes it easier to add and 
delete alias definitions later.

<BR></P>

<P>After you've defined an alias, you might want to list the aliases in effect to see your new definition. Simply enter the alias command with no arguments. For example,

<BR></P>

<PRE>$ alias

true=let 1

false=let 0

lx=ls -FC</PRE>

<P>In all likelihood, there are a good many more alias definitions in effect than you defined. The Korn shell automatically defines a number of aliases when it starts up&#151;such as when you log in&#151;to provide convenient abbreviations for some Korn 
shell commands. The true and false definitions fall into this category. The UNIX operating system provides true and false commands, but as programs they must be searched for and loaded into memory to execute. As aliases the shell can execute them much more 

quickly, so these two particular aliases are provided as an easy performance enhancement for the many shell scripts you execute&#151;usually unknowingly&#151;throughout the day.

<BR></P>

<P>To use the lx command alias previously shown, use it as a new command name. For example,

<BR></P>

<PRE>$ lx</PRE>

<P>by itself lists all the files in the current directory in a neat columnar format, sorted for easy inspection. To list a directory other than the current directory, use the command

<BR></P>

<PRE>$ lx /usr/bin</PRE>

<P>After alias substitution, the shell sees the command ls -FC /usr/bin.

<BR></P>

<P>The ability to prespecify command options in an alias is a great help. Even better, you can usually augment or alter prespecified command options when you use the alias. Suppose, for example, that you want to add the command option -a when listing 
/usr/bin so that you can see all dot files in the directory. You might think that you have to type the full ls command, because the lx alias doesn't include an a option letter. Not so. The following command works quite well:

<BR></P>

⌨️ 快捷键说明

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