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

📄 0415-0418.html

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTML
字号:




<HTML>

<HEAD>

<TITLE>Developer.com - Online Reference Library - 0672311739:RED HAT LINUX 2ND EDITION:Shell Programming</TITLE>

<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>

 -->




<!-- ISBN=0672311739 //-->

<!-- TITLE=RED HAT LINUX 2ND EDITION //-->

<!-- AUTHOR=DAVID PITTS ET AL //-->

<!-- PUBLISHER=MACMILLAN //-->

<!-- IMPRINT=SAMS PUBLISHING //-->

<!-- PUBLICATION DATE=1998 //-->

<!-- CHAPTER=21 //-->

<!-- PAGES=0411-0436 //-->

<!-- UNASSIGNED1 //-->

<!-- UNASSIGNED2 //-->









<P><CENTER>

<a href="0411-0414.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0419-0422.html">Next</A>

</CENTER></P>



<A NAME="PAGENUM-415"><P>Page 415</P></A>













<P>If you want to assign the value of var to the variable

lcount, you can do that as follows:

</P>



<TABLE WIDTH="360">

<TR><TD>

Command

</TD><TD>

Environment

</TD></TR>

</TABLE>

<HR>



<PRE>

lcount=$var                   pdksh and bash







set lcount = $var             tcsh



</PRE>











<H3><A NAME="ch21_ 7">

Positional Parameters

</A></H3>









<P>It is possible to write a shell script that takes a number of parameters at the time you invoke

it from the command line or from another shell script. These options are supplied to the

shell program by Linux as positional parameters. The positional parameters have special names

provided by the system. The first parameter is stored in a variable called

1 (number 1) and can be accessed by using $1 within the program. The second parameter is stored in a variable called

2 and can be accessed by using $2 within the program, and so on. It is possible to omit one

or more of the higher numbered positional parameters while invoking a shell program. For

example, if a shell program mypgm expects two parameters&#151;such as a first name and a last

name&#151;then you can invoke the shell program with only one parameter, the first name. However,

you cannot invoke it with only the second parameter, the last name.

</P>









<P>Here's a shell program called mypgm1, which takes only one parameter (a name) and displays

it on the screen:

</P>





<!-- CODE //-->

<PRE>

#Name display program

if [ $# == 0]

then

   echo &quot;Name not provided&quot;

else

   echo &quot;Your name is &quot;$1

</PRE>

<!-- END CODE //-->













<P>In pdksh and bash, if you execute mypgm1 as follows:

</P>





<!-- CODE SNIP //-->

<PRE>

. mypgm1

</PRE>

<!-- END CODE SNIP //-->











<P>you will get the output:

</P>





<!-- CODE SNIP //-->

<PRE>

Name not provided

</PRE>

<!-- END CODE SNIP //-->











<P>However, if you execute mypgm1 as follows:

</P>





<!-- CODE SNIP //-->

<PRE>

. mypgm1 Sanjiv

</PRE>

<!-- END CODE SNIP //-->











<P>then you will get the following output:

</P>





<!-- CODE SNIP //-->

<PRE>

Your name is Sanjiv

</PRE>

<!-- END CODE SNIP //-->











<P>The shell program mypgm1 also illustrates another aspect of shell programming, the

built-in variables. In mypgm1, the variable $# is a built-in variable and provides the number of

positional parameters passed to the shell program.

</P>





<A NAME="PAGENUM-416"><P>Page 416</P></A>











<H3><A NAME="ch21_ 8">

Built-in Variables

</A></H3>









<P>Built-in variables are a special type of variable that Linux provides to you. These variables

can be used to make decisions within a program. You cannot modify the values of these

variables within the shell program.

</P>









<P>Some of these variables are

</P>





<TABLE WIDTH="360">

<TR><TD>

$#

</TD><TD>

Number of positional parameters passed to the shell program

</TD></TR>



<TR><TD>

$?

</TD><TD>

Code of the last command or shell program executed within

the shell program

</TD></TR>



<TR><TD>

$0

</TD><TD>

The name of the shell program

</TD></TR>



<TR><TD>

$*

</TD><TD>

A single string of all arguments passed at the time of

invocation of the shell program

</TD></TR>

</TABLE>









<P>To show these built-in variables in use, here is a sample program called

mypgm2:

</P>





<!-- CODE //-->

<PRE>

#my test program

echo &quot;Number of parameters is &quot;$#

echo &quot;Program name is &quot;$0

echo &quot;Parameters as a single string is &quot;$*

</PRE>

<!-- END CODE //-->











<P>In pdksh and bash, if you execute mypgm2 from the command line, as follows:

</P>





<!-- CODE SNIP //-->

<PRE>

. mypgm2 Sanjiv Guha

</PRE>

<!-- END CODE SNIP //-->











<P>you will get the following result:

</P>







<!-- CODE SNIP //-->

<PRE>

Number of parameters is 2

Program name is mypgm2

Parameters as a single string is Sanjiv Guha

</PRE>

<!-- END CODE SNIP //-->











<H3><A NAME="ch21_ 9">

Special Characters

</A></H3>









<P>Some characters have special meaning to Linux shells, so using them as part of variable

names or strings will cause your program to behave incorrectly. If a string contains such special

characters, then you also have to use escape characters to indicate that the special characters

should not be treated as special characters.

</P>









<P>Some of these special characters are shown in the following table.

</P>



<TABLE WIDTH="360">

<TR><TD>

Character

</TD><TD>

Explanation

</TD></TR>>



<TR><TD>

$

</TD><TD>

Indicates the beginning of a shell variable name

</TD></TR>



<TR><TD>

|

</TD><TD>

Pipes standard output to the next command

</TD></TR>



<TR><TD>

#

</TD><TD>

Starts a comment

</TD></TR>



<TR><TD>

&amp;

</TD><TD>

Executes a process in the background

</TD></TR>



<TR><TD>

?

</TD><TD>

Matches one character

</TD></TR>

</TABLE>





<A NAME="PAGENUM-417"><P>Page 417</P></A>





<TABLE WIDTH="360">

<TR><TD>

Character

</TD><TD>

Explanation

</TD></TR>



<TR><TD>

*

</TD><TD>

Matches one or more characters

</TD></TR>



<TR><TD>

&gt;

</TD><TD>

Output redirection operator

</TD></TR>



<TR><TD>

&lt;

</TD><TD>

Input redirection operator

</TD></TR>



<TR><TD>

`

</TD><TD>

Command substitution (the backquote or backtick&#151;the

key above the Tab key)

</TD></TR>



<TR><TD>

&gt;&gt;

</TD><TD>

Output redirection operator (to append to a file)

</TD></TR>



<TR><TD>

[ ]

</TD><TD>

Lists a range of characters

</TD></TR>



<TR><TD>

[a-z]

</TD><TD>

Means all characters a through z

</TD></TR>



<TR><TD>

[a,z]

</TD><TD>

Means characters a or z

</TD></TR>



<TR><TD>

. filename

</TD><TD>

Executes the file filename

</TD></TR>



<TR><TD>

Space

</TD><TD>

Delimiter between two words

</TD></TR>

</TABLE>











<P>There are a few special characters that deserve special note. They are the double quotes

(&quot;), the single quote (`), the backslash (\), and the backtick

(`). They are discussed in the following sections.

</P>









<H4><A NAME="ch21_ 10">





Double Quotes

</A></H4>









<P>If a string contains embedded spaces, you can enclose the string in double quotes

(&quot;) so that the shell interprets the whole string as one entity instead of more than one. For example, if

you assigned the value of abc def (abc followed by one space followed by

def) to a variable called x in a shell program as follows:

</P>



<TABLE WIDTH="360">

<TR><TD>

Command

</TD><TD>

Environment

</TD></TR>

</TABLE>

<HR>

<PRE>

x=abc def                pdksh and bash







set x = abc def          tcsh

</PRE>













<P>you would get an error as the shell would try to execute

def as a separate command. What you need to do is surround the string in double quotes as follows:

</P>



<TABLE WIDTH="360">

<TR><TD>

Command

</TD><TD>

Environment

</TD><TD>

</TABLE>

<HR>

<PRE>

x=&quot;abc def&quot;             pdksh and bash







set x  = &quot;abc def&quot;       tcsh

</PRE>













<P>The double quotes resolve all the variables within the string. Here is an example for

pdksh and bash:

</P>





<!-- CODE SNIP //-->

<PRE>

var =&quot;test string&quot;

newvar=&quot;Value of var is $var&quot;

echo $newvar

</PRE>

<!-- END CODE SNIP //-->







<A NAME="PAGENUM-418"><P>Page 418</P></A>











<P>Here is the same example for tcsh:

</P>





<!-- CODE SNIP //-->

<PRE>

set var = &quot;test string&quot;

set newvar = &quot;Value of var is $var&quot;

echo $newvar

</PRE>

<!-- END CODE SNIP //-->











<P>If you execute a shell program containing these three lines, you will get the following

result:

</P>





<!-- CODE SNIP //-->

<PRE>

Value of var is test string

</PRE>

<!-- END CODE SNIP //-->











<H4><A NAME="ch21_ 11">





Single Quote

</A></H4>









<P>You can use the single quote (`) to surround a string in order to stop the shell from

resolving a variable. In the following example, the double quotes in the preceding example have

been changed to single quotes.

</P>





<!-- CODE //-->

<PRE>

pdksh and bash:

var ='test string'

newvar='Value of var is $var'

echo $newvar

tcsh:

set var = `test string'

set newvar = `Value of var is $var'

echo $newvar

</PRE>

<!-- END CODE //-->











<P>If you execute a shell program containing these three lines, you will get the following result:

</P>





<!-- CODE SNIP //-->

<PRE>

Value of var is $var

</PRE>

<!-- END CODE SNIP //-->











<P>As you can see, the variable var did not get resolved.

</P>









<H4><A NAME="ch21_ 12">





Backslash

</A></H4>









<P>You can use a backslash (\) before a character to stop the shell from interpreting the

following character as a special character. Say you want to assign a value of

$test to a variable called var. If you use the following command:

</P>



<TABLE WIDTH="360">

<TR><TD>

Command

</TD><TD>

Environment

</TD></TR>

</TABLE>

<HR>



<PRE>

var=$test              pdksh and bash







set var = $test         tcsh

</PRE>













<P>then a null value will be stored in var. This is because the shell will interpret

$test as the value of the variable test, and as

test has not been assigned any value, var will contain null.

You should use the following command to correctly store

$test in var:

</P>



<P><CENTER>

<a href="0411-0414.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0419-0422.html">Next</A>

</CENTER></P>









</td>
</tr>
</table>

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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