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

📄 0411-0414.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="../ch20/0407-0410.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0415-0418.html">Next</A>

</CENTER></P>



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











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

CHAPTER 21

</A></H3>













<H2>



Shell Programming



</H2>









<P>by Sanjiv Guha

</P>









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

IN THIS CHAPTER

</A></H3>









<UL>

<LI>     Creating and Executing a Shell Program  412



<LI>     Variables  413



<LI>     Positional Parameters  415



<LI>     Built-in Variables  416



<LI>     Special Characters  416



<LI>     Comparison of Expressions  419



<LI>     Iteration Statements  426



<LI>     Conditional Statements  430



<LI>     Miscellaneous Statements  433



<LI>     Functions 434

</UL>





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













<P>When you enter commands from the command line, you are entering commands one at a

time and getting the response from the system. From time to time you will need to execute

more than one command, one after the other, and get the final result. You can do so with a

shell program or shell script. A shell program is a series of Linux commands and utilities that

have been put into a file using a text editor. When you execute a shell program, each command

is interpreted and executed by Linux, one after the other.

</P>









<P>You can write shell programs and execute them like any other command under Linux. You

can also execute other shell programs from within a shell program if they are in the search path.

A shell program is like any other programming language and has its own syntax. You can

have variables defined and assign various values and so on. These are discussed in this

chapter.

</P>









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

Creating and Executing a Shell Program

</A></H3>









<P>Say you want to set up a number of aliases every time you log on. Instead of typing all

the aliases every time you log on, you can put them in a file using a text editor, such as

vi, and then execute the file.

</P>









<P>Here is a list of what is contained in a sample file created for this purpose,

myenv:

</P>





<!-- CODE SNIP //-->

<PRE>

alias ll `ls _l'

alias dir `ls'

alias copy `cp'

</PRE>

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











<P>myenv can be executed in a variety of ways under Linux.

</P>









<P>You can make myenv executable by using the

chmod command as follows, and then execute it as you would any other native Linux command:

</P>





<!-- CODE SNIP //-->

<PRE>

chmod +x myenv

</PRE>

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











<P>This turns on the executable permission of

myenv. There is one more thing you need to ensure before you can execute

myenv. The file myenv must be in the search path. You can get the

search path by executing

</P>





<!-- CODE SNIP //-->

<PRE>

echo $PATH

</PRE>

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











<P>If the directory where the file myenv is located is not in the current search path, then you

must add the directory name in the search path.

</P>









<P>Now you can execute the file myenv from the command line, as if it were a Linux command:

</P>





<!-- CODE SNIP //-->

<PRE>

myenv

</PRE>

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









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



<TABLE BGCOLOR=#FFFF99><TR><TD>NOTE</TD></TR><TR><TD><BLOCKQUOTE>

You must ensure that the first line in your shell program starts with a pound sign

(#). The pound sign tells the shell that the line is a comment. Following the pound sign, you

must have an exclamation point (!), which tells the shell to run the command following

the exclamation point and to use the rest of the file as input for that command. This is

common practice for all shell scripting.

</BLOCKQUOTE>

</TD></TR></TABLE>



<P>A second way to execute myenv under a particular shell, such as

pdksh, is as follows:

</P>





<!-- CODE SNIP //-->

<PRE>

pdksh myenv

</PRE>

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











<P>This invokes a new pdksh shell and passes the filename

myenv as a parameter to execute the file.

</P>









<P>You can also execute myenv from the command line as follows:

</P>



<TABLE WIDTH="360">

<TR><TD>

Command

</TD><TD>

Environment

</TD></TR>

</TABLE>

<HR>

<PRE>

. myenv                  pdksh and bash







source myenv             tcsh



</PRE>











<P>The dot (.) is a way of telling the shell to execute the file

myenv. In this case, you do not have to ensure that execute permission of the file is set. Under

tcsh, you have to use the source command instead of the dot

(.) command.

</P>









<P>After executing the command myenv, you should be able to use

dir from the command line to get a list of files under the current directory, and

ll to get a list of files with various attributes displayed.

</P>









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

Variables

</A></H3>









<P>Linux shell programming is a full-fledged programming language and as such supports

various types of variables. There are three major types of variables: environment, built-in, and user.

</P>









<P>Environment variables are part of the system environment, and you do not have to define

them. You can use them in your shell program; some of them, like

PATH, can also be modified within a shell program.

</P>









<P>Built-in variables are provided by the system; unlike environment variables, you cannot

modify them.

</P>









<P>User variables are defined by you when you write a shell script. You can use and modify

them at will within the shell program.

</P>





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













<P>A major difference between shell programming and other programming languages is that

in shell programming, variables are not typecast; that is, you do not have to specify whether

a variable is a number or a string and so on.

</P>









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





Assigning a Value to a Variable

</A></H4>









<P>Say you want to use a variable called lcount to count the number of iterations in a loop

within a shell program. You can declare and initialize this variable as follows:

</P>



<TABLE WIDTH="360">

<TR><TD>

Command

</TD><TD>

Environment

</TD></TR>

</TABLE>

<HR>

<PRE>



lcount=0                  pdksh and bash







set lcount =              0tcsh

</PRE>



<TABLE BGCOLOR=#FFFF99><TR><TD>NOTE</TD></TR><TR><TD>

<BLOCKQUOTE>

Under pdksh and bash, you must ensure that the equal sign

(=) does not have space before and after it.

</BLOCKQUOTE>

</TD></TR></TABLE>



<P>As shell programming languages do not use typed variables, it is possible that the same

variable can be used to store an integer value one time and a string another time. However, this is

not recommended. You should be careful not to do this.

</P>









<P>To store a string in a variable, you can use the following:

</P>



<TABLE WIDTH="360">

<TR><TD>

Command

</TD><TD>

Environment

</TD></TR>

</TABLE>

<HR>

<PRE>

myname=Sanjiv                   pdksh and bash







set myname = Sanjiv             csh

</PRE>













<P>The preceding can be used if the string does not have embedded spaces. If a string has

embedded spaces, you can do the assignment as follows:

</P>



<TABLE WIDTH="360">

<TR><TD>

Command

</TD><TD>

Environment

</TD></TR>

</TABLE>

<HR>

<PRE>

myname='Sanjiv Guha'                 pdksh and bash







set myname = `Sanjiv Guha'           tcsh



</PRE>









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





Accessing Variable Values

</A></H4>









<P>You can access the value of a variable by prefixing the variable name by a

$ (dollar sign). That is, if the variable name is

var, you can access the variable by using $var.

</P>





<P><CENTER>

<a href="../ch20/0407-0410.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0415-0418.html">Next</A>

</CENTER></P>









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

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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