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

📄 unx12.htm

📁 Linux Unix揭密.高质量电子书籍.对学习Linux有大帮助,欢迎下载学习.
💻 HTM
📖 第 1 页 / 共 5 页
字号:

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

<P>When the shell executes this command, it immediately replaces lx with the alias value string, obtaining the following internal form:

<BR></P>

<PRE>$ ls -FC -a /usr/bin</PRE>

<P>The ls command, like most other UNIX commands, is comfortable with command options specified in multiple words. In effect, the -a option has been added to the -FC options provided automatically by the alias.

<BR></P>

<H4 ALIGN="CENTER">

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

<FONT SIZE=3><B>Removing an Alias</B>

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

<P>To remove an alias that you or the Korn shell previously defined, use the unalias command:

<BR></P>

<PRE>$ unalias name [ name ... ]</PRE>

<P>Notice that just as you can define multiple aliases on one command line, you also can remove multiple aliases with one unalias command.

<BR></P>

<H4 ALIGN="CENTER">

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

<FONT SIZE=3><B>Writing an Alias Definition</B>

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

<P>One of my favorite aliases is the following one for the pg command:

<BR></P>

<PRE>$ alias pg='/usr/bin/pg -cns -p&quot;Page %d:&quot;'</PRE>

<P>The pg alias is instructive in a number of ways. Take a look at it in detail.

<BR></P>

<P>First, note that the alias name is pg. This is the same as the pg command itself, so in effect the alias hides the pg command. You can invoke the real UNIX pg command by using an explicit pathname&#151;calling /usr/bin/pg&#151;but not by the short 
command pg, which invokes the alias instead.

<BR></P>

<P>Choosing the same name for an alias as a real command name is unusual. It implies that you never want to execute the real command directly, and that you always want to dress it up with the options specified in the alias.

<BR></P>

<P>Because of the way I work, the options -c, -n, -s, and -p should have been built in to the command; I always want to use them. The -c option causes pg to clear the screen when it displays a new page. On a video terminal, this is more natural and faster 

than scrolling the lines. The -n option causes pg to execute a command key immediately without waiting for the Enter key. All pg commands are one letter long. The only reason not to use the -n option is to avoid the slack in performance that results from 
generating a terminal interrupt for each keypress, which the -n option requires. However, single-user workstations and modern high-performance computers don't notice the extra workload. Therefore, unless you're working on an old PDP-11, go ahead and 
specify the -n option for the convenience it adds. The -s option displays messages, such as the current page number, in highlighted mode, usually inverse video, which makes the non-text part of the display easier to notice or to ignore.

<BR></P>

<P>The -p option causes the pg command to display the page number at the bottom of each screen. I like page numbering because it gives me a rough idea of where I am in the displayed document. By default, the page number is displayed as a bare number, run 
on with the rest of the command line. The pg command, however, enables you supply a format for the page number. I specified -p&quot;Page %d:&quot;. It identifies the page number with the word Page and provides a colon (:) to separate the page number from 
the input command line.

<BR></P>

<P>Because the page number format string contains characters special to the shell&#151;specifically, an embedded blank&#151;it must be enclosed in quotes. The alias command also requires that the entire alias definition be enclosed in quotes. Therefore, I 

need a quote within a quote.

<BR></P>

<P>If you understood the discussion of quotes in the chapter on the Bourne shell, you should also realize that there are at least three ways to write this alias command:

<BR></P>

<PRE>$ alias pg='/usr/bin/ls -cns -p&quot;Page %d:&quot;'

$ alias pg=&quot;/usr/bin/ls -cns -p'Page %d'&quot;

$ alias pg=&quot;/usr/bin/ls -cns -p\&quot;Page %d\&quot;&quot;</PRE>

<P>The first form is the form I chose for the example. The second embeds a single quoted string inside a double quoted string; it works just as well. The third form uses an escape character to embed a double quote inside a double quoted string. In this 
case, the shell strips off the backslashes before it stores the alias value. I avoid this form, because I don't like to use escape sequences unless I have to.

<BR></P>

<P>The point here is that alias definitions usually must be enclosed in quotes&#151;unless the alias value is a single word. Thus, you must occasionally embed quoted strings inside a quoted string. You should recognize that this need can arise. Be prepared 

to handle it by making sure that you understand how the shell quoting mechanism works.

<BR></P>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="caution.gif" WIDTH = 37 HEIGHT = 35><B>CAUTION:</B> If you do get a handle on how the shell quoting syntax works, it incites many otherwise nice people to brand you as a UNIX guru. So be careful.

<BR></NOTE>

<HR ALIGN=CENTER>

<H4 ALIGN="CENTER">

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

<FONT SIZE=3><B>Using Exported Aliases</B>

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

<P>The alias command supports a number of options, including -x (export)  and -t (tracking).

<BR></P>

<P>An exported alias is much the same concept as an exported variable. Its value is passed into shell scripts that you invoke.

<BR></P>

<P>Exporting a command alias can be both helpful and harmful. For example, exporting the pg alias shown earlier would be helpful, because it would cause pg commands issued by a shell script&#151;many UNIX commands are implemented as shell scripts&#151;to 
work as I prefer. On the other hand, if you define an alias for the rm command that always prompts before deleting a file, you might be inundated with requests from system-supplied shell scripts to delete temporary files that you never heard of.

<BR></P>

<P>Use the command alias -x to display only those command aliases that are exported. Used in the form alias -x name, the alias name is redefined as an exported alias; it should have been defined previously. To define a new exported alias, use the full form 

alias -x name=value.

<BR></P>

<H4 ALIGN="CENTER">

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

<FONT SIZE=3><B>Using Tracked Aliases</B>

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

<P>By default, the Korn shell creates a tracked alias entry automatically for many of the commands that you invoke from the keyboard. This helps to improve performance. When an alias is tracked, the Korn shell remembers the directory where the command is 
found. Therefore, subsequent invocations don't have to search your PATH list for the command file. Essentially, the alias for the command is simply set to the full pathname of the command.

<BR></P>

<P>You can display the commands for which a tracked alias exists by using the command alias -t.

<BR></P>

<P>To request explicit tracking for a command that you use frequently, use the form alias -t name. If no alias already exists with the given name, the Korn shell does a path search and stores the full pathname of the command name as the alias value. 
Otherwise, the shell simply marks the alias as tracked for future reference.

<BR></P>

<P>Note that you generally don't set the tracked attribute for command aliases that you write&#151;that is, where the alias name differs from the alias value. The values for tracked aliases should usually be set by the Korn shell itself. You can achieve 
the effect of a tracked alias by supplying the full pathname of the command in the alias value. This eliminates path searches. For example, the lx alias shown earlier would be better written as alias lx='/usr/bin/ls -FC'; it would achieve the same effect 
as tracking.

<BR></P>

<P>As a final example, suppose that the vi command is not in the list when you issue the command alias -t, but that you know you will be using the command fairly frequently. To request tracking for the vi command, simply issue the command alias -t vi.

<BR></P>

<P>One of the major reasons for the name tracking is that the Korn shell takes account of the possibility that your search path&#151;the value of the PATH shell variable&#151;may include the directory . (dot), a reference to your current directory. If you 

switch to another directory, commands that were available might become unavailable, or they might need to be accessed by a different pathname. Alias tracking interacts with the cd command to keep the full pathnames of tracked aliases current. In other 
words, alias tracking keeps track of the proper full pathname for commands as you switch from directory to directory and create, remove, or relocate executable files.

<BR></P>

<H3 ALIGN="CENTER">

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

<FONT SIZE=4><B>Shell Options</B>

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

<P>Being a rather sophisticated program, the Korn shell deals with many human interface issues that might be resolved in two or more ways. To help you use the shell in ways most convenient to you, the shell enables you to choose how it behaves by setting 
options.

<BR></P>

<P>There are two ways to set Korn shell options: on the ksh command when you invoke the shell and on the set command from within the shell once you've got it started. Options that you don't set explicitly take on a default value. Thus, you never need to 
bother with option settings unless you want to.

<BR></P>

<P>The ksh command is normally issued on your behalf by the UNIX login processor, using a template stored in the /etc/passwd file for your login name. Generally, the system administrator constructs the password entry for you, but unless he's very busy or 
very mean-spirited, he'll be happy to adjust your password entry to invoke the shell with your preferred settings. Of course, you can replace your login shell with the Korn shell at any time by using the following command:

<BR></P>

<PRE>$ exec ksh options ...</PRE>

<P>The exec statement that you encountered in your study of the Bourne shell does the same thing under the Korn shell. It replaces the current shell with the command named as its first argument&#151;usually also a shell, but perhaps of a different type or 

with different options and arguments.

<BR></P>

<P>The syntax of the ksh command is

<BR></P>

<PRE>ksh [ [pm]aefhkmnpstuvx- ] [-cirs] [[pm]o option] ... [[pm]A name] [arg ...]</PRE>

<P>The -c, -i, -r, and -s options can be specified only on the ksh command line. All the other options can be specified on the set command as well.

<BR></P>

<P>The options specifiable only on the ksh command line are

<BR></P>

<TABLE BORDER>

<TR>

<TD>

<P>-c</P>

<TD>

<P>Command: The first (and only) arg is a command. The -c option prevents the shell from attempting to read commands from any other source. It merely executes the command given as arg and then exits. This option is not used often from the keyboard or from 

within shell scripts. It is most often used internally by programs written in the C language.</P>

<TR>

<TD>

<P>-i</P>

<TD>

<P>Interactive shell: Forces the shell to behave as though its input and output are a terminal. Usually, you don't need to specify the -i option explicitly. Its main purpose is to prevent the abnormal termination of commands invoked by the shell from 
terminating the shell itself.</P>

<TR>

<TD>

<P>-r</P>

<TD>

<P>Restricted shell: The Korn shell runs as a restricted shell and prevents the user from using the cd command or from invoking a command by its full pathname. This option is normally of interest only to the system administrator for setting up specialized 

user accounts.</P>

<TR>

<TD>

<P>-s</P>

<TD>

<P>Standard input: The Korn shell doesn't activate the protections against abnormal termination given by option -i. The shell reads commands from standard input until end of file and then exits normally. This is a handy option, because it enables you to 
pipe a stream of commands to the shell for execution.</P></TABLE>

<P>Additional options that you may specify on either the ksh command or the set command are listed below. Options can be specified with a letter in the usual way&#151;for example, -a&#151;or by name&#151;for example, -o allexport. An option that has been 
set, either explicitly or by default, can be turned off with the + flag&#151;as in +a or +o allexport.

<BR></P>

<TABLE BORDER>

<TR>

<TD>

<P>-a</P>

<TD>

<P>The equivalent option name is allexport. All variables are treated implicitly as exported variables. You don't need to invoke the typeset -x command or export alias to export the variable. A variable becomes eligible for export when it is first defined, 

whether by the typeset statement or by an assignment statement. The typeset-x command and export alias are permitted, but they have no additional effect.</P>

<TR>

<TD>

<P>-e</P>

<TD>

<P>The equivalent option name is errexit. Any command returning a non-zero exit code causes immediate termination of the shell. When it is set within a shell script, only the shell script is terminated.</P>

<TR>

<TD>

<P>-f</P>

<TD>

<P>The equivalent option name is noglob. Filename expansion is disabled. Wildcard expressions are treated literally and, with the -f option in force, have no special meaning or effect. You might use set -f and set +f to disable wildcard expansion for a 
short range of statements.</P>

<TR>

<TD>

<P>-h</P>

<TD>

<P>The equivalent option name is trackall. Every command issued is automatically defined as a tracked alias, just as though you executed alias -t xxx in front of each command. The -h option is set on by default for non-interactive shells. Commands that 
specify a full pathname or that use names not valid as command alias names are not tracked.</P>

<TR>

<TD>

<P>-k</P>

<TD>

<P>The equivalent option name is keyword. When -k is set, command arguments having the form name=value are stripped from the command line and are executed as assignment statements before the command is executed. The assignment is temporarily exported for 
the duration of the one command. The effect is equivalent to adding keyword arguments to the shell language and to UNIX commands and shell scripts that support this kind of argument. Most UNIX commands and shell scripts, however, do not support keyword 
arguments. Therefore, the -k option has little real application.</P>

<TR>

<TD>

<P>-m</P>

⌨️ 快捷键说明

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