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

📄 unx11.htm

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

<BR></P>

<PRE>$ UNAME=

$ echo ${UNAME:?}

sh: UNAME: parameter null or not set

$</PRE>

<H4 ALIGN="CENTER">

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

<FONT SIZE=3><B>Positional Variables or Parameters</B>

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

<P>You may recall that when the shell's command-line interpreter processes a line of input, the first word of the command line is considered to be an executable, and the remainder of the line is passed as arguments to the executable. If the executable is a 

shell program, the arguments are passed to the program as positional variables. The first argument passed to the program is assigned to the variable $1, the second argument is $2, and so on up to $9. Notice that the names of the variables are actually the 

digits 1 through 9; the dollar sign, as always, is the special character that causes variable substitution to occur.

<BR></P>

<P>The positional variable $0 always contains the name of the executable. Positional variables are discussed later in this chapter in the section &quot;Shell Programming.&quot;

<BR></P>

<H5 ALIGN="CENTER">

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

<FONT SIZE=3><B>Special Variables in the Bourne Shell</B>

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

<P>The Bourne shell defines several variables that are always available. (You can find examples of how you can use these variables in the next major section of this chapter, &quot;Shell Programming.&quot;) Following are some of the predefined variables:

<BR></P>

<TABLE BORDER>

<TR>

<TD>

<P>$#</P>

<TD>

<P>Contains the number of arguments passed to the program in the form of positional variables.</P>

<TR>

<TD>

<P>$$</P>

<TD>

<P>Contains the process ID of the current process.</P>

<TR>

<TD>

<P>$?</P>

<TD>

<P>Contains the exit status of the last command executed. Programs and commands customarily return an exit status of zero if the program is successful and return a nonzero value if an error occurs. But be careful, not all programs follow customs.</P>

<TR>

<TD>

<P>$*</P>

<TD>

<P>Contains all the positional arguments passed to the program.</P></TABLE>

<H5 ALIGN="CENTER">

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

<FONT SIZE=3><B>Environment Variables</B>

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

<P>Environment variables are variables that the shell or any other program can access to get information unique to a specific user. Any program can use environment variables. The vi editor, for example, checks the variable EXINIT for any standard options 
you want set every time you run vi. Consult the instructions of the specific programs for information on environment variables used by the program.

<BR></P>

<P>Several environment variables are created and used by the Bourne shell.

<BR></P>

<PRE>

<BR>HOME This variable is initialized when the interactive shell is executed by the login program. It contains the value of the user's home directory. If the cd command is executed without any arguments, the effect is cd $HOME.



<BR>IFS This variable sets characters to be internal field separators, or the characters that separate words on a command line. By default, the internal field separators are the space, tab, and newline characters. Setting the IFS adds separators, but 
space, tab, and the newline character always separate fields.



<BR>MAIL This variable specifies the complete pathname of the user's mailbox file.



<BR>MAILCHECK This variable specifies in seconds how often the mailbox should be checked for incoming mail.



<BR>MAILPATH This variable is a colon-separated list of mailbox files to be checked. Setting this variable can be useful for users who have more than one login name but want to have their mail checked, regardless of which name they have used to log in. The 

name of the mail file can be followed with a percent sign (%) and a message to be displayed when mail is received in that file.



<BR>PATH This variable, usually set in your .profile, contains a list of directories that are searched for executables. If an executable, which is any utility, program, or shell program, is not in the PATH variable, it can only be executed by giving the 
full pathname of the executable file. The directories are searched in the order in which they appear in the PATH variable. If an executable file exists in more that one directory, the one found earliest in PATH is the one executed. See &quot;Customizing 
the Shell&quot; later in this chapter.



<BR>PS1 The value of the interactive shell prompt. The default value in the Bourne Shell is $.



<BR>PS2 The value of the secondary shell prompt. The default value is &gt;.



<BR>TERM This variable is not automatically created on all UNIX systems but is used by so many programs that it is considered a standard environment variable. TERM usually contains the type of terminal that you are using, such as ansi or vt100.</PRE>

<H5 ALIGN="CENTER">

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

<FONT SIZE=3><B>Preventing Variables from Being Changed</B>

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

<P>If a variable has had a value assigned, and you want to make sure that its value is not subsequently changed, you may designate a variable as a readonly variable with the following command:

<BR></P>

<PRE>readonly <I>variable</I></PRE>

<P>From this point on, <I>variable</I> cannot be reassigned. This ensures that a variable won't be accidentally changed.

<BR></P>

<H5 ALIGN="CENTER">

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

<FONT SIZE=3><B>Making Variables Available to Subshells with </B><B><I>export</I></B>

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

<P>When a shell executes a program, it sets up a new environment for the program to execute in. This is called a subshell. In the Bourne shell, variables are considered to be local variables; in other words, they are not recognized outside the shell in 
which they were assigned a value. You can make a variable available to any subshells you execute by exporting it using the export command. Your variables can never be made available to other users.

<BR></P>

<P>Suppose you wanted to change your shell prompt to something more meaningful than a dollar sign. You could do this by assigning a new value to the shell prompt variable PS1.

<BR></P>

<PRE>$ PS1=&quot;Enter Command: &quot;

Enter Command: </PRE>

<P>Now, instead of a dollar sign, you get the descriptive phrase Enter Command: . Now suppose you start a new shell.

<BR></P>

<PRE>Enter Command: <B>sh</B>

$ exit

Enter Command: </PRE>

<P>When you started a new shell, the default shell prompt appeared. This is because the variable assignment to PS1 was made only in the current shell. To make the new shell prompt active in subshells, you must export it as in the following example.

<BR></P>

<PRE>$ PS1=&quot;Enter Command: &quot;

Enter Command: <B>export PS1</B>

Enter Command: <B>sh</B>

Enter Command: </PRE>

<P>Now the variable PS1 is global, that is, it is available to all subshells. When a variable has been made global in this way, it remains available until you log out of the parent shell. You can make an assignment permanent by including it in your 
.profile, see &quot;Customizing the Shell.&quot;

<BR></P>

<H3 ALIGN="CENTER">

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

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

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

<P>In this major section, you learn how to put commands together in such a way that the sum is greater than the parts. You learn some UNIX commands that are useful mainly in the context of shell programs. You also learn how to make your program perform 
functions conditionally based on logical tests that you define, and you learn how to have parts of a program repeat until its function is completed. In short, you learn how to use the common tools supplied with UNIX to create more powerful tools specific 
to the tasks you need to perform.

<BR></P>

<H4 ALIGN="CENTER">

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

<FONT SIZE=3><B>What Is a Program?</B>

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

<P>A wide assortment of definitions exist for what is a computer program, but for this discussion, a computer program is an ordered set of instructions causing a computer to perform some useful function. In other words, when you cause a computer to perform 

some tasks in a specific order so that the result is greater than the individual tasks, you have programmed the computer. When you enter a formula into a spreadsheet, for example, you are programming. When you write a macro in a word processor, you are 
programming. When you enter a complex command like

<BR></P>

<PRE>$ ls -R / | grep myname | pg</PRE>

<P>in a UNIX shell, you are programming the shell; you are causing the computer to execute a series of utilities in a specific order, which gives a result that is more useful than the result of any of the utilities taken by itself.

<BR></P>

<H4 ALIGN="CENTER">

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

<FONT SIZE=3><B>A Simple Program</B>

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

<P>Suppose that daily you back up your data files with the following command:

<BR></P>

<PRE>$ cd /usr/home/myname; ls * | cpio -o &gt;/dev/rmt0</PRE>

<P>As you learned earlier, when you enter a complex command like this, you are programming the shell. One of the useful things about programs, though, is that they can be placed in a program library and used over and over, without having to do the 
programming each time. Shell programs are no exception. Rather than enter the lengthy backup command each time, you can store the program in a file named backup:

<BR></P>

<PRE>$ cat &gt;backup

cd /usr/home/myname

ls * | cpio -o &gt;/dev/rmt0

Ctrl+d</PRE>

<P>You could, of course, use your favorite editor (see Chapter 7, &quot;Editing Text Files&quot;), and in fact with larger shell programs, you almost certainly will want to. You can enter the command in a single line, as you did when typing it into the 
command line, but because the commands in a shell program (sometimes called a shell script) are executed in sequence, putting each command on a line by itself makes the program easier to read. Creating easy-to-read programs becomes more important as the 
size of the programs increase.

<BR></P>

<P>Now to back up your data files, you need to call up another copy of the shell program (known as a subshell) and give it the commands found in the file backup. To do so, use the following command:

<BR></P>

<PRE>$ sh backup</PRE>

<P>The program sh is the same Bourne shell that was started when you logged in, but when a filename is passed as an argument, instead of becoming an interactive shell, it takes its commands from the file.

<BR></P>

<P>An alternative method for executing the commands in the file backup is to make the file itself an executable. To do so, use the following command:

<BR></P>

<PRE>$ chmod +x backup</PRE>

<P>Now you can back up your data files by entering the newly created command:

<BR></P>

<PRE>$ backup</PRE>

<P>If you want to execute the commands in this manner, the file backup must reside in one of the directories specified in the environment variable $PATH.

<BR></P>

<H4 ALIGN="CENTER">

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

<FONT SIZE=3><B>The Shell as a Language</B>

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

<P>If all you could do in a shell program was to string together a series of UNIX commands into a single command, you would have an important tool, but shell programming is much more. Like traditional programming languages, the shell offers features that 
enable you to make your shell programs more useful, such as: data variables, argument passing, decision making, flow control, data input and output, subroutines, and handling interrupts.

<BR></P>

<P>By using these features, you can automate many repetitive functions, which is, of course, the purpose of any computer language.

<BR></P>

<H4 ALIGN="CENTER">

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

<FONT SIZE=3><B>Using Data Variables in Shell Programs</B>

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

<P>You usually use variables within programs as place holders for data that will be available when the program is run and that may change from execution to execution. Consider the backup program:

<BR></P>

<PRE>cd /usr/home/myname

ls | cpio -o &gt;/dev/rmt0</PRE>

<P>In this case, the directory to be backed up is contained in the program as a literal, or constant, value. This program is useful only to back up that one directory. The use of a variable makes the program more generic:

<BR></P>

<PRE>cd $WORKDIR

ls * | cpio -o &gt;/dev/rmt0</PRE>

<P>With this simple change, any user can use the program to back up the directory that has been named in the variable $WORKDIR, provided that the variable has been exported to subshells. See &quot;Making Variables Available to Subshells with export&quot; 
earlier in this chapter.

<BR></P>

<H4 ALIGN="CENTER">

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

<FONT SIZE=3><B>Entering Comments in Shell Programs</B>

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

<P>Quite often when you're writing programs, program code that seemed logical six months ago may be fairly obscure today. Good programmers annotate their programs with comments. You enter comments into shell programs by inserting the pound sign (#) special 

character. When the shell interpreter sees the pound sign, it considers all text to the end of the line as a comment.

<BR></P>

<H4 ALIGN="CENTER">

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

<FONT SIZE=3><B>Doing Arithmetic on Shell Variables</B>

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

<P>In most higher level programming languages, variables are typed, meaning that they are restricted to certain kinds of data, such as numbers or characters. Shell variables are always stored as characters. To do arithmetic on shell variables, you must use 

the expr command.

<BR></P>

<P>The expr command evaluates its arguments as mathematical expressions. The general form of the command is as follows:

<BR></P>

<PRE>expr integer operator integer</PRE>

<P>Because the shell stores its variables as characters, it is your responsibility as a shell programmer to make sure that the integer arguments to expr are in fact integers. Following are the valid arithmetic operators:

<BR></P>

<PRE>

<BR>+ Adds the two integers.



<BR>- Subtracts the second integer from the first.



<BR>* Multiplies the two integers.



<BR>/ Divides the first integer by the second.



<BR>% Gives the modulus (remainder) of the division.

$ expr 2 + 1

3

$ expr 5 - 3

2</PRE>

<P>If the argument to expr is a variable, the value of the variable is substituted before the expression is evaluated, as in the following example:

<BR></P>

<PRE>$ $int=3

$ expr $int + 4

7</PRE>

<P>You should avoid using the asterisk operator (*) alone for multiplication. If you enter

<BR></P>

<PRE>$ expr 4 * 5</PRE>

<P>you get an error because the shell sees the asterisk and performs filename substitution before sending the arguments on to expr. The proper form of the multiplication expression is

<BR></P>

<PRE>$ expr 4 \* 5

20</PRE>

<P>You also can combine arithmetic expressions, as in the following:

<BR></P>

<PRE>$ expr 5 + 7 / 3

7</PRE>

⌨️ 快捷键说明

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