免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1725 | 回复: 0
打印 上一主题 下一主题

shell编程简介 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-03-30 12:29 |只看该作者 |倒序浏览
1.1 shell编程概述1.2 shell程序举例1.3 传递数据给shell程序1.4 shell 程序的参数1.5 一些特殊shell变量- #和*1.6 shift 命令1.7 read 命令1.8 另外的技术
1.1 shell编程概述
shell程序是一个包含UNIX命令的普通文件。
这个文件的许可权限至少应该为可读和可执行。
在shell提示符下键入文件名就可执行shell程序。
shell程序可以通过三种方式接受数据:
??-环境变量
??-命令行参数
??-用户的输入
shell是一个命令解释器,它会解释并执行命令提示符下输入的命令。但是,你可能想要多次执行一组命令,shell提供了一种功能,让你将这组命令存放在一个文件中,然后你可以象unix系统提供的其他程序一样执行这个文件,这个命令文件就叫做shell程序或者shell脚本。当你运行这个文件,它会象你在命令行输入这些命令一样地执行这些命令。为了让shell能读取并且执行你的shell程序,shell脚本的文件权限必须被设置为可读和可执行。为了让shell可以找到你的程序,你可以选择输入完全路径名,或者将这个脚本的路径放在于你的PATH环境变量指定的路径列表中。许多的用户会在他们的HOME目录下创建一个bin目录来存放他们自己开发的script,然后将$HOME/bin加入到他们的PATH环境变量中。你可以写出非常复杂的shell脚本,因为shell脚本支持变量、命令行参数、交互式输入、tests(判断))、branches(分支),和loops(循环)等复杂的结构。
1.2 shell程序举例
$ cat myprog
#this is the program myprog
date
ls –F
$ myprog
要创建一个shell程序,考虑进行以下步骤:
$ vi myprog ?????一个包含shell命令的程序。
#this is the program myprog
date
ls –F
$ chmod +x myprog 增加文件的执行模式
$ myprog
Thu Jul 11 11:10 EDT 1994
F1 f2 memo/ myprog*
首先使用一个文本编辑器创建一个shell程序myprog。在程序执行之前,这个文件必须被赋予可执行的权限。然后在命令提示符下输入这个程序名,如上例所示,当myprog执行的时候,一个子shell会被创建。这个子shell会从shell程序文件myprog读取输入而不是从命令行读取输入,这个shell中的每个命令的执行都会创建一个子shell。一旦所有的命令都被执行,所有的子shell会中止,然后会返回到原始的父shell。
Shell程序中的注释:
推荐在shell程序中提供注释语句来注明程序的内容。注释由一个#符号开始,Shell不会去执行任何在#之后的语句。#能够出现在命令行的任何位置。
注意:你不可以给shell程序取名为test因为test是一个内部的shell命令。
1.3 传递数据给shell程序
$ color = lavender
$ cat color1
echo you are now running program: color1
echo the value of the variable color is : $color
$ chmod +x color1
$ color1
you ar now running program : color1
the value of the variable color is :
$ export color
$ color1
you are now running program : color1
the value of the variable color is : lavender
传递数据给shell脚本的一种方法就是通过环境。在上例中,本地变量color被赋值为lavender,然后创建了shell程序color1;然后更改为可执行权限;然后这个shell程序被执行,color1脚本的意图是显示color变量的值,但是由于color是一个本地变量,属于父shell私有的,运行color1产生的子shell不能识别这个变量,因此不能打印出它的值,而当color被输出到环境中就可以被子shell读取。
同样,由于shell进程不能够更改父进程的环境,对一个子进程中的环境变量重新赋值不会影响到父进程环境中的值。如以下的shell脚本中的color2。
echo The original value of the variable color is $color
ech0 This program will set the value of color to amber
color=amber
echo The value of color is now $color
echo When your program concludes,display the value of the color variable
观察在你设置了color的值后有什么变化。输出这个变量,然后执行color2:
$ export color=lavender
$ echo $color
lanvender
$ color2
The original value of the variable color is lavender
The program will set the value of color to amber
The value of volor is now amber
When your progam concludes, display the value of the color variable,
$ echo $color
lanvender
1.4 shell 程序的参数
命令行:
?$ sh_program arg1 arg2 . . . argx
???$0 ???$1?? $2 ....? $X
例子:
$ cat color3
echo you are now running program: $0
echo The value of command line argument #1 is: $1
echo The value of command line argument #2 is : $2
$ chmod +x color3
$ color3 red green
You are now running program: color3
The value of command line argument #1 is : red
The value of command line argument #2 is: green
大多数的UNIX系统命令可以接收命令行参数,这些参数通常告诉命令它将要操作的文件或目录(cp f1 f2),另外指定的参数扩展命令的能力(ls –l),或者提供文本字符串(banner hi there)。
命令行参数对shell程序同样有效,使用这种方式传送信息给你的程序十分方便。通过开发一个接收命令行参数的程序,你可以传递文件或者目录命令名给你的程序处理,就像你运行UNIX系统命令一样,你也可以定义命令行选项来让命令行使用shell程序额外的功能。
在shell程序中的命令行参数与参数在命令行的位置相关,这样的参数被称为位置参数,因为对每一个特殊变量的赋值依靠一这些参数在命令行中的位置,变量的变量名对应变量在命令行中的位置,因此这些特殊的变量名为数字0,1,2等,一直到最后的参数被传递,变量名的存取也通过同样的方法,在名字前面加上$ 符号,因此,为了存取你的shell程序中的命令行参数,你可以应用$0,$1,$2等等。在$9以后,必须使用括号:$(10),$(11),否则,shell会将$10看成是$1后面跟一个0。而$0会一直保存程序或命令的名字
以下的shell程序会安装一个程序,这个程序作为一个命令行参数被安装到你的bin目录:首先创建程序my_install,注意目录$HOME/bin应该预先存在。
$ cat > my_install
echo $0 will install $1 to your bin directory
chmod +x $1
mv $1 $HOME/bin
echo Installation of $1 is complete
ctrl + d
$ chmod +x my_intalll
$ my_install color3
my_install will install color3 to your bin directory
Installation of color3 is complete
$
这个例子中,程序指明第一个命令行参数为一个文件名,然后加上执行权限,然后移动到你当前目录下的bin目录下。
记住UNIX系统的惯例是存贮程序在bin的目录下。你也许想要在你的HOME目录下创建一个bin目录,在这个目录下你可以存储你的程序文件,记住要将你的bin目录放在PATH环境变量中,这样shell才会找到你的程序。
1.5 一些特殊shell变量- #和*
# ???命令行参数的数量
* ???完全的参数字符串
例子:
$ cat color4
echo There are $#?comand line argument
echo They are $*
ehco The first command line argument is $1
$ chmod +x color4
$ color4 red green yellow blue
They are 4 command line arguments
They are red green yellow blue
The first command line argument is red
$
至今为止我们看到的shell程序都不是很灵活,如color3需要输入两个正确的参数而my_install只需要一个。通常在创建一个接收命令行参数的shell程序的时候,你想要用户输入一个参数的变量号码。你同时要程序执行成功,不管用户键入1个参数或是20个参数。当处理变量参数列表的时候,特殊shell变量会提供你许多的灵活性。通过$#你可以知道有多少参数已经被输入,通过$*可以存取全部的参数列表,而不管参数的数量。请注意参数($0)不在$*这个参数列表里。
每一个命令行参数都是互相独立的,你可以通过$*集中检索这些参数,也可以通过$1,$2,$3等等来独立的检索这些参数。
一个可以接收多个命令行参数的安装程序的例子:
$ cat > my_install2
echo $0 will install $# files to your bin directory
echo The files to be installed are : $*
chmod +x $*
mv $* $HOME/bin
echo Installaton is complete
ctril + d
$ chmod +x my_install2
$ my_install2 color1 color2
my_intall2 will install 2 files to your bin directory
The files to be installed are: color1,color2
Intallaiton is complete
这个安装程序更加灵活,如果你有多个文件要安装,你仅需要执行这个程序一次,只要一次输入多个名字即可。
非常重要的是:如果你计划传递整个参数的字符串给一个命令,这个命令必须能够接收多个参数。
在以下的脚本中,用户提供一个目录名作为一个命令行参数。程序会更改到指定的目录,显示当前的位置,并且列出内容。
$ cat list_dir
cd $*
echo You are in the $(pwd) directory
echo The contents of the directory are:
ls –F
$ list_dir dir1 dir2 dir3
sh: cd: bad argument count
由于cd命令不能同时进入到多个目录中,这个程序执行会出错。
1.6 shift 命令
向左移动所有的在*中的字符串n个位置
#的数目减少n个(n的默认值是1)
语法:shift [n]
例子:
$ cat color5
orig_args=$*
echo There are $# command line arguments
echo They are $*
echo Shifting two arguments
shift 2
echo There are $# comand line arguments
echo They are $*
echo Shifting two arguments
shift 2; final_args=$*
echo Original arguments are: $orig_args
echo Final arguments are: $final_args
shift命令会重新分配命令行参数对应位置参数,在shift n以后,所有的*中的参数会向左移动n个位置。同时$#会减n。默认的n为1。Shift命令不会影响到参数0的位置。一旦你完成一次移动,被移出命令行的参数会丢失。如果你想在你的程序中引用这个参数,你需要在执行shift之前存贮这个参数到一个变量中。
Shift命令可以用于:存取一组参数的位置,例如一系列的x,y的坐标
从命令行删除命令选项,假定选项在参数之前。
例子:
$ color5 red green yellow orange black
There are 6 command line arguments
They are red green yellow blue orange black
Shifting two arguments
There are 4 command line arguments
They are yellow blue orange black
Shiftging two arguments
Original arguments are: red green yellow blue orange black
Final argument are : orange black
$
1.7 read 命令
语法:
read variable [variable......]
例子:
$ cat color6
echo This program prompts for user input
echo “please enter your favorite two colors -> c”
read color_a color_b
echo The colors you entered are: $color_b $color_a
$ chmod +x color6
$ color6
This program prompts for user input
Please enter your favorite two colors -> red blue
The colors you entered are: blue red
$ color6
This program prompts for user input
Please enter you favorite two colors -> red blue tan
The color you enterd are :blue tan red
如果使用命令行参数传递信息进程序,在命令执行之前用户必须知道正确的语法。有一种情况,你想要在用户执行程序的时候提示他输入这些参数。read命令就是用来在程序执行的时候收集终端键入的信息。
通常会使用echo命令来给用户一个提示,让他知道程序正在等待一些输入,同时通知用户应该输入的类型。因此,每一个read命令应该在echo命令后面。
read命令会给出一个变量名的列表,用户在提示符下输入会给这些变量赋值(变量之间以空格分隔)。如果read命令定义的变量比输入的词要多,多出的变量会被赋空值。如果用户输入的词要比变量多,剩余的数据会赋给列表中的最后一个变量。
一旦被赋值,你就可以象其他的shell变量一样存取这些变量。
注意:不要混淆位置参数和变量read。位置参数在命令被激活时直接在命令行中使用,而read命令给变量赋值是在程序执行之中,用户响应输入的提示而给变量赋值。
以下例子提示用户输入要被安装的文件名:
$ cat > my_install3
echo $0 will install files into your bin directory
echo “Enter the names of the files -> c”
read filenames
mv $filenames $HOME/bin
echo Instllation is complete
ctrl + d
$ chmod +x my_install13
$ my_install13
my_install13 will install files into your bin directory
Enter the names of the files -> f1 f2
Installaton is complete
这个安装脚本会提示用户输入需要chmod并移动到$HOME/bin的文件的文件名。这个程序给用户更多的关于应该输入数据情况的指引。而不像install2中用户必须在命令行中提供文件名,用户使用程序不需要特殊的语法,程序让用户确切地知道要输入什么。所有的输入的文件名都会被赋值给变量filenames。
1.8 另外的技术
#号开始的文档为注释部分。
sh shell_program argumetns?
shell_program 的属性可以不是可执行的。
shell_program 必须是可读的。
sh –x shell_program arguments
每一行在被执行前被打印出来。
在调试程序时有用处。
在shell程序中,#符号的意思是后面是一段注释,而shell会自动忽略#符号以后直到一个回车符号为止的所有字符。<

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/5587/showart_18502.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP