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

📄 _chapter 10.htm

📁 linux、unix初学者的必读书籍 详细讲述了shell编程方法与技巧
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<html>

<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Chapter 10</title>
<link rel="stylesheet" type="text/css" href="docsafari.css">
<link rel="stylesheet" type="text/css" href="style.css">
</head>

<body><table width="100%" border="1" bgcolor="#EBEBFF"><tr><td width="5%" align="left" valign="middle"><a href="_chapter 9.htm"><img src="Larrow.gif" width="17" height="19" border="0"></a></td><td align="center" valign="middle"><a class="docLink" href="Front matter.htm">CONTENTS</a></td><td width="5%" align="right" valign="middle"><a href="_chapter 11.htm"><img src="Rarrow.gif" width="17" height="19" border="0"></a></td></tr></table>


<h2 class="docChapterTitle">Chapter 10. The Korn Shell</h2><ul><li>&nbsp;<a class="docLink" href="#ch10lev1sec1">10.1 Interactive Korn Shell</a></li>
<li>&nbsp;<a class="docLink" href="#ch10lev1sec2">10.2 Programming with the Korn Shell</a></li>
<li>&nbsp;<a class="docLink" href="#ch10lev1sec3">KORN SHELL LAB EXERCISES</a></li>
</ul>
<center>
<p class="docText">
<img alt="graphics/ch10.gif" src="ch10.gif" border="0" width="484" height="613"></p>
</center>
<h3 class="docSection1Title" id="ch10lev1sec1">10.1 Interactive Korn Shell</h3>
<p class="docText">Before the Korn shell displays a prompt, it is preceded by a
number of processes. See <a class="docLink" href="#ch10fig01">Figure 10.1</a>.</p>
<center>
<h5 id="ch10fig01" class="docFigureTitle">Figure 10.1. System startup and the Korn shell.</h5>
<p class="docText">
<img alt="graphics/10fig01.gif" src="10fig01.gif" border="0" width="420" height="496"></p>
</center>
<h4 class="docSection2Title" id="ch10lev2sec1">10.1.1 Startup</h4>
<p class="docText">The first process to run is called <span class="docEmphasis">
init,</span> PID 1. It gets instructions from a file called
<span class="docEmphasis">inittab</span> (System V) or spawns a
<span class="docEmphasis">getty</span> (BSD) process. These processes open up
the terminal ports, provide a place where input comes from,
<span class="docEmphasis">stdin,</span> and the place where standard output (<span class="docEmphasis">stdout</span>)
and standard error (<span class="docEmphasis">stderr</span>) go, and put a login
prompt on your screen. The <span class="docEmphasis">/bin/login</span> program
is then executed. The <span class="docEmphasis">login</span> program prompts you
for a password, encrypts and verifies the password, sets up an initial
environment, and starts up the login shell, <span class="docEmphasis">/bin/ksh,</span>
the last entry in the <span class="docEmphasis">passwd</span> file. The
<span class="docEmphasis">ksh</span> program looks for the system file,
<span class="docEmphasis">/etc/profile,</span> and executes its commands. It
then looks in the user's home directory for an initialization file called
<span class="docEmphasis">.profile,</span> and an environment file,
conventionally called <span class="docEmphasis">.kshrc.</span> After executing
commands from those files, the dollar sign prompt appears on your screen and the
Korn shell awaits commands.</p>
<h4 class="docSection2Title" id="ch10lev2sec2">10.1.2 The Environment</h4>
<p class="docText"><b>The Initialization Files.</b> After executing the commands
in <span class="docEmphasis">/etc/profile,</span> the initialization files in
the user's home directory are executed. The <span class="docEmphasis">.profile</span>
is executed, followed by the <span class="docEmphasis">ENV</span> file, commonly
called the <span class="docEmphasis">.kshrc</span> file.</p>
<p class="docText"><span class="docEmphStrong">The <span class="docEmphasis">
/etc/profile</span> File.</span> The <span class="docEmphasis">/etc/profile</span>
is a systemwide readable file set up by the system administrator to perform
tasks when the user logs on and the Korn shell starts up. It is available to all
Bourne and Korn shell users on the system, and normally performs such tasks as
checking the mail spooler for new mail and displaying the message of the day
from the <span class="docEmphasis">/etc/motd</span> file. The following text is
an example of the /etc/<span class="docEmphasis">profile.</span> See
<a class="docLink" href="_chapter 8.htm">Chapter 8, &quot;The
Interactive Bourne Shell,&quot;</a> for a complete explanation of each line of
<span class="docEmphasis">/etc/profile.</span></p>
<pre>EXAMPLE
<span class="docEmphasis"># The profile that all logins get before using their own .profile</span>
    trap &quot; &quot; 2 3
    export LOGNAME PATH  <span class="docEmphasis"># Initially set by /bin/login</span>
    if [ &quot;$TERM&quot; = &quot; &quot; ]
    then
       if /bin/i386
       then     <span class="docEmphasis"># Set the terminal type</span>
          TERM=AT386
       else
          TERM=sun
       fi
       export TERM
    fi
    <span class="docEmphasis"># Login and -su shells get /etc/profile services.</span>
    <span class="docEmphasis"># -rsh is given its environment in the .profile.</span>
    case &quot;$0&quot; in
    -sh | <span class="docEmphStrong">-ksh</span> | -jsh )
        if [ ! -f .hushlogin ]
        then
          /usr/sbin/quota
       <span class="docEmphasis">#</span> Allow the user to break the Message-Of-The-Day only.
          trap &quot;trap ' ' 2&quot; 2
          /bin/cat -s /etc/motd
          <span class="docEmphasis"># Display the message of the day</span>
          trap &quot; &quot; 2
          /bin/mail -E
          case $? in
          0)        <span class="docEmphasis"># Check for new mail</span>
              echo &quot;You have new mail. &quot;
                   ;;
          2)  echo &quot;You have mail. &quot;
                   ;;
          esac
       fi
    esac
    umask 022
    trap 2 3
</pre>
<p class="docText"><span class="docEmphStrong">The <span class="docEmphasis">
.profile</span> File.</span> The <span class="docEmphasis">.profile</span> file
is a user-defined initialization file, that is executed once at login (by the
Bourne and Korn shells) and is found in your home directory. It gives you the
ability to customize and modify your working environment. Environment variables
and terminal settings are normally set here, and if a window application or
<span class="docEmphasis">dbm</span> is to be initiated, it is started here. If
the <span class="docEmphasis">.profile</span> file contains a special variable
called <span class="docEmphasis">ENV,</span> the filename that is assigned to
that variable will be executed next. The <span class="docEmphasis">ENV</span>
file is often named <span class="docEmphasis">.kshrc;</span> it contains aliases
and <span class="docEmphasis">set 杘</span> commands. The
<span class="docEmphasis">ENV</span> file is executed every time a
<span class="docEmphasis">ksh</span> subshell is spawned. The lines from the
following files may not be meaningful to you now, but all of the concepts, such
as exporting variables, history, the search path, and so on, will be discussed
in detail throughout the text of this book.</p>
<h5 id="ch10list01" class="docExampleTitle">Example 10.1 </h5>
<pre>1   set -o allexport
2   TERM=vt102
3   HOSTNAME=$(uname -n)
4   HISTSIZE=50
5   EDITOR=/usr/ucb/vi
6   ENV=$HOME/.kshrc
7   PATH=$HOME/bin:/usr/ucb:/usr/bin:\
    /usr/local:/etc:/bin:/usr/bin:/usr/local\
    /bin:/usr/hosts:/usr/5bin:/usr/etc:/usr/bin:.
8   PS1=&quot;$HOSTNAME ! $ &quot;
9   set +o allexport
10  alias openwin=/usr/openwin/bin/openwin
11  trap '$HOME/.logout' EXIT
12  clear
</pre>

<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <span style="FONT-WEIGHT: bold">
    <ol class="docList" type="1">
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">By setting the <span class="docEmphasis">allexport</span>
      option, all variables created will automatically be exported (made
      available to subshells).</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The terminal is set to <span class="docEmphasis">vt102.</span></span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The variable <span class="docEmphasis">HOSTNAME</span>
      is assigned the name of this machine, <span class="docEmphasis">$(uname
      杗).</span></span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The <span class="docEmphasis">HISTSIZE</span> variable
      is assigned <span class="docEmphasis">50;</span> 50 lines from the history
      file will be displayed on the terminal when the user types
      <span class="docEmphasis">history.</span></span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The <span class="docEmphasis">EDITOR</span> variable is
      assigned the pathname for the <span class="docEmphasis">vi</span> editor.
      Programs such as <span class="docEmphasis">mail</span> allow you to select
      an editor in which to work.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The <span class="docEmphasis">ENV</span> variable is
      assigned the path to the home directory (<span class="docEmphasis">$HOME</span>)
      and the name of the file that contains further Korn shell customization
      settings. After the <span class="docEmphasis">.profile</span> is executed,
      the <span class="docEmphasis">ENV</span> file is executed. The name of the
      <span class="docEmphasis">ENV</span> file is your choice; it is commonly
      called <span class="docEmphasis">.kshrc.</span></span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The search path is defined. It is a colon-separated
      list of directories used by the shell in its search for commands typed at
      the prompt or in a script file. The shell searches each element of the
      path from left to right for the command. The dot at the end represents the
      current working directory. If the command cannot be found in any of the
      listed directories, the shell will look in the current directory.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The primary prompt, by default a dollar sign ($), is
      set to the name of the host machine, the number of the current command in
      the history file, and a dollar sign (<span class="docEmphasis">$</span>).</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The <span class="docEmphasis">allexport</span> option
      is turned off.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">An alias is a nickname for a command. The alias for
      <span class="docEmphasis">openwin</span> is assigned the full pathname of
      the <span class="docEmphasis">openwin</span> command, which starts Sun's
      window application.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The <span class="docEmphasis">trap</span> command will
      execute the <span class="docEmphasis">.logout</span> file when you exit
      this shell, that is, when you log out. The <span class="docEmphasis">
      .logout</span> file is a user-defined file containing commands that are
      executed at the time of logging out. For example, you may want to record
      the time you log out, clean up a temporary file, or simply say
      <span class="docEmphasis">So long.</span></span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The <span class="docEmphasis">clear</span> command
      clears the screen.</span></li>
    </ol>
    </span></td>
  </tr>
</table>

<p class="docText"><span class="docEmphStrong">The <span class="docEmphasis">ENV</span>
File.</span> The <span class="docEmphasis">ENV</span> variable is assigned the
name of a file that will be executed every time an interactive
<span class="docEmphasis">ksh</span> or <span class="docEmphasis">ksh</span>
program (script) is started. The <span class="docEmphasis">ENV</span> variable
is set in the <span class="docEmphasis">.profile</span> and is assigned the name
of the file that will contain special <span class="docEmphasis">ksh</span>
variables and aliases. The name is conventionally <span class="docEmphasis">.kshrc,</span>
but you can call it anything you want. (The <span class="docEmphasis">ENV</span>
file is not processed when the privileged option is on. See
<a class="docLink" href="#ch10table01">Table 10.1</a>.)</p>
<h5 id="ch10list02" class="docExampleTitle">Example 10.2 </h5>
<pre>1   set -o trackall
2   set -o vi
3   alias l='ls -laF'
    alias ls='ls -aF'
    alias hi='fc -l'
    alias c=clear
4   function pushd { pwd &gt; $HOME/.lastdir.$$ ; }
    function popd { cd $(&lt; $HOME/.lastdir.$$) ;
           rm $HOME/.lastdir.$$; pwd; }
    function psg { ps -ef | egrep $1 | egrep -v egrep; }
    function vg { vgrind -s11 -t $* | lpr -t ; }
</pre>

<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <span style="FONT-WEIGHT: bold">
    <ol class="docList" type="1">
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The <span class="docEmphasis">set</span> option for
      tracked aliases is turned on. (For a complete description, see &quot;<a class="docLink" href="#ch10lev2sec8">Aliases</a>&quot;.)</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The <span class="docEmphasis">set</span> option for the
      <span class="docEmphasis">vi</span> editor is turned on for in-line
      editing of the history file. (See &quot;<a class="docLink" href="#ch10lev2sec6">Command
      Line History</a>&quot;.)</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The aliases (nicknames) for the commands are defined.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The functions are named and defined. (See &quot;<a class="docLink" href="#ch10lev2sec16">Functions</a>&quot;.)</span></li>
    </ol>
    </span></td>
  </tr>
</table>

<p class="docText"><span class="docEmphStrong">The <span class="docEmphasis">set
杘</span> Options.</span> The <span class="docEmphasis">set</span> command can
take options if the <span class="docEmphasis">杘</span> switch is used. Options
allow you to customize the shell environment. They are either on or off, and are
normally set in the <span class="docEmphasis">ENV</span> file.</p>

<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">FORMAT</h2>
    <pre>set -o option  Turns on the option.
set +o option  Turns off the option
set -[a-z] Abbreviation for an option; the minus turns it on
set +[a-z] Abbreviation for an option; the plus turns it off
</pre>
    </td>
  </tr>
</table>

<h5 id="ch10list03" class="docExampleTitle">Example 10.3 </h5>
<pre>set -o allexport
set +o allexport
set -a
set +a
</pre>

<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <span style="FONT-WEIGHT: bold">
    <ol class="docList" type="1">
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">Sets the <span class="docEmphasis">allexport</span>
      option. This option causes all variables to be automatically exported to
      subshells.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">Unsets the <span class="docEmphasis">allexport</span>
      option. All variables will now be local to the current shell.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">Sets the <span class="docEmphasis">allexport</span>
      option. Same as 1. Not every option has an abbreviation (see

⌨️ 快捷键说明

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