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

📄 unx11.htm

📁 Unix Unleashed, Third Edition is written with the power user and system administrator in mind. This
💻 HTM
📖 第 1 页 / 共 5 页
字号:
Line 2

Ctrl+d

$</PRE>

<P>So if you wanted to send a short note to John, you might type:

<BR></P>

<PRE>$ mail John

John,

   Meet me at the mall at noon.

Rick

Ctrl+d

$</PRE>

<H3 ALIGN="CENTER">

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

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

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

<P>The Bourne shell is a computer program and like most programs it has several options. You are already familiar with the most common shell option, the interactive shell. Some options change the way the shell interprets command lines; others put limits on 

the user of the shell

<BR></P>

<H4 ALIGN="CENTER">

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

<FONT SIZE=3><B>The Restricted Shell</B>

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

<P>The restricted shell gives more control to the system administrator and restricts the options of the user. The restricted shell is useful in situations where security is vital or where the users lack sophistication. The restricted shell can be a user's 

default login shell. On many systems, the restricted shell is invoked by using /usr/bin/rsh, but this may vary; consult your system's documentation. You may also invoke the restricted shell by using the -r flag when you're invoking the shell:

<BR></P>

<PRE>$ sh -r</PRE>

<P>In a restricted shell, the user cannot change directories (cd), change the PATH variable, specify a full pathname to a command, or redirect output.

<BR></P>

<P>The restricted user can execute shell programs that have access to these features. If the restricted shell calls a shell procedure, an unrestricted shell is invoked to carry out the commands. In this case, if the user has write permission in his or her 

working directory, he or she can write shell programs and bypass the restrictions. Normally, a restricted user is placed in a directory in which he or she has no write permission. Not having write permission in this directory does not mean that the user 
has no write permission anywhere, but because he or she cannot change directories or specify pathnames in commands, the user cannot write a shell script and later access it if he or she cannot write in the working directory.

<BR></P>

<H5 ALIGN="CENTER">

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

<FONT SIZE=3><B>Changing Shell Options with </B><B><I>set</I></B>

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

<P>Although the restricted shell and the interactive shell are chosen when the shell is invoked, you can turn other options on and off using the set option. Following are some options you can set:

<BR></P>

<TABLE BORDER>

<TR>

<TD>

<P>-e</P>

<TD>

<P>Causes a noninteractive shell to exit if any subsequent command terminates with a nonzero exit status</P>

<TR>

<TD>

<P>-f</P>

<TD>

<P>Disables filename substitution</P>

<TR>

<TD>

<P>-n</P>

<TD>

<P>Causes the shell to read commands but not execute them</P>

<TR>

<TD>

<P>-u</P>

<TD>

<P>Treats unset variables as errors when substituting</P>

<TR>

<TD>

<P>-x</P>

<TD>

<P>Prints commands and their arguments as they are executed, showing the result of any substitutions</P></TABLE>

<P>You turn on options with a hyphen (-) and turned them off with a plus (+).

<BR></P>

<P>For example, the shell normally looks at command line input and tries to substitute filenames when it encounters certain special characters, such as an asterisk (*). This default behavior can be changed with the set command using the -f option.

<BR></P>

<PRE>$ set -f

$ echo *

*</PRE>

<P>You can restore the default behavior by using set with the +f option.

<BR></P>

<PRE>$ set +f

$ echo *

file1 file2 ...</PRE>

<H3 ALIGN="CENTER">

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

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

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

<P>In algebra, variables are symbols which stand for some value. In computer terminology, variables are symbolic names which stand for some value. Earlier in this chapter, you saw how the variable HOME stood for the name of a user's home directory. If you 

enter the change directory command, cd, without an argument, cd takes you to your home directory. Does a generic program like cd know the location of every user's home directory? Of course not, it merely knows to look for a variable, in this case HOME, 
which stands for the home directory.

<BR></P>

<P>Variables are useful in any computer language because they allow you to define what to do with a piece of information without knowing specifically what the data is. A program to add two and two together is not very useful, but a program that adds two 
variables can be, especially if the value of the variables can be supplied at execution time by the user of the program. The Bourne shell has four types of variables: user-defined variables, positional variables or parameters, predefined or special 
variables, and environment variables.

<BR></P>

<H4 ALIGN="CENTER">

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

<FONT SIZE=3><B>Defining Your Own (User-Defined) Variables</B>

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

<P>As the name implies, user-defined variables are whatever you want them to be. Variable names are made up of alphanumeric characters and the underscore character, with the provision that variable names do not begin with one of the digits 0 through 9. 
(Like all UNIX names, variables are case sensitive. Variable names take on values when they appear in a command line to the left of an equal sign (=). For example, in the following command lines, COUNT takes on the value of 1, and NAME takes on the value 
of Stephanie:

<BR></P>

<PRE>$ COUNT=1

$ NAME=Stephanie</PRE>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="imp.gif" WIDTH = 68 HEIGHT = 35><B>TIP:</B> Because most UNIX commands are lowercase words, shell programs have traditionally used all capital letters in variable names. It is certainly not mandatory to use all capital letters, but using them 
enables you to identify variables easily within a program.

<BR></NOTE>

<HR ALIGN=CENTER>

<P>To recall the value of a variable, precede the variable name by a dollar sign ($):

<BR></P>

<PRE>$ NAME=John

$ echo Hello $NAME

Hello John</PRE>

<P>You also can assign variables to other variables, as follows:

<BR></P>

<PRE>$ JOHN=John

$ NAME=$JOHN

$ echo Goodbye $NAME

Goodbye John</PRE>

<P>Sometimes it is useful to combine variable data with other characters to form new words, as in the following example:

<BR></P>

<PRE>$ SUN=Sun

$ MON=Mon

$ TUE=Tues

$ WED=Wednes

$ THU=Thurs

$ FRI=Fri

$ SAT=Satur

$ WEEK=$SAT

$ echo Today is $WEEKday

Today is

$</PRE>

<P>What happened here? Remember that when the shell's interpreter sees a dollar sign ($), it interprets all the characters until the next white space as the name of a variable, in this case WEEKday. You can escape the effect of this interpretation by 
enclosing the variable name in curly braces ({,}) like this:

<BR></P>

<PRE>$ echo Today is ${WEEK}day

Today is Saturday

$</PRE>

<P>You can assign more than one variable in a single line by separating the assignments with white space, as follows:

<BR></P>

<PRE>$ X=x Y=y</PRE>

<P>The variable assignment is performed from right to left:

<BR></P>

<PRE>$ X=$Y Y=y

$ echo $X

y

$ Z=z Y=$Z

$ echo $Y

$</PRE>

<P>You may notice that when a variable that has not been defined is referenced, the shell does not give you an error but instead gives you a null value.

<BR></P>

<P>You can remove the value of a variable using the unset command, as follows:

<BR></P>

<PRE>$ Z=hello

$ echo $Z

hello

$ unset Z

$ echo $Z

$</PRE>

<H4 ALIGN="CENTER">

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

<FONT SIZE=3><B>Conditional Variable Substitution</B>

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

<P>The most common way to retrieve the value of a variable is to precede the variable name with a dollar sign ($), causing the value of the variable to be substituted at that point. With the Bourne shell, you can cause variable substitution to take place 
only if certain conditions are met. This is called conditional variable substitution. You always enclose conditional variable substitutions in curly braces ({ }).

<BR></P>

<H5 ALIGN="CENTER">

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

<FONT SIZE=3><B>Substituting Default Values for Variables</B>

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

<P>As you learned earlier, when variables that have not been previously set are referenced, a null value is substituted. The Bourne shell enables you to establish default values for variable substitution using the form

<BR></P>

<PRE>${variable:-value}</PRE>

<P>where <I>variable</I> is the name of the variable and <I>value</I> is the default substitution. For example,

<BR></P>

<PRE>$ echo Hello $UNAME

Hello

$ echo Hello ${UNAME:-there}

Hello there

$ echo $UNAME

$

$ UNAME=John

$ echo Hello ${UNAME:-there}

Hello John

$</PRE>

<P>As you can see in the preceding example, when you use this type of variable substitution, the default value is substituted in the command line, but the value of the variable is not changed. Another substitution construct not only substitutes the default 

value but also assigns the default value to the variable as well. This substitution has the form

<BR></P>

<PRE>${variable:=value}</PRE>

<P>which causes <I>variable</I> to be assigned <I>value</I> after the substitution has been made. For example,

<BR></P>

<PRE>$ echo Hello $UNAME

Hello

$ echo Hello ${UNAME:=there}

Hello there

$ echo $UNAME

there

$ UNAME=John

$ echo Hello ${UNAME:-there}

Hello John

$</PRE>

<P>The substitution value need not be literal; it can be a command in back quotation marks:

<BR></P>

<PRE>USERDIR={$MYDIR:-'pwd'}</PRE>

<P>A third type of variable substitution substitutes the specified value if the variable has been set, as follows:

<BR></P>

<PRE>${variable:+value}</PRE>

<P>If <I>variable</I> is set, then <I>value</I> is substituted; if <I>variable</I> is not set, then nothing is substituted. For example,

<BR></P>

<PRE>$ ERROPT=A

$ echo ${ERROPT:+&quot;Error Tracking is Active&quot;}

Error Tracking is Active

$ ERROPT=

$ echo ${ERROPT:+&quot;Error Tracking is Active&quot;}

$</PRE>

<H5 ALIGN="CENTER">

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

<FONT SIZE=3><B>Conditional Variable Substitution with Error Checking</B>

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

<P>Another variable substitution method allows for error checking during variable substitution:

<BR></P>

<PRE>${variable:?message}</PRE>

<P>If <I>variable</I> is set, its value is substituted; if it is not set, <I>message</I> is written to the standard error file. If the substitution is made in a shell program, the program immediately terminates. For example,

<BR></P>

<PRE>$ UNAME=

$ echo ${UNAME:?&quot;UNAME has not been set&quot;}

UNAME has not been set

$ UNAME=Stephanie

$ echo ${UNAME:?&quot;UNAME has not been set&quot;}

Stephanie

$</PRE>

<P>If no message is specified, the shell displays a default message, as in the following example:

⌨️ 快捷键说明

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