没有合适的资源?快使用搜索试试~ 我知道了~
shell编程-shell编程
需积分: 9 170 下载量 82 浏览量
2008-11-27
09:28:25
上传
评论
收藏 223KB PDF 举报
温馨提示
试读
22页
shell编程简介 shell编程概述 shell程序举例
资源推荐
资源详情
资源评论
shell编程-shell编程简介
介绍 shell 编程
目标:
完成这一章,你能做以下事情:
写出简单的 shell 程序
通过环境变量传递参数给 shell 程序
通过位置参数传递参数给 shell 程序
使用特殊 shell 变量,*和#
使用 shift 和 read 命令
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(判断))、br
anches
(分支),和 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。
S
hell
程序中的注释:
推荐在 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
$ c
olor2
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 会一直
保存程序或命令的名字
1.4 shell 程序的参数(继续)
以下的 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 等等来独立的检索这些参数。
1.5 一些特殊的 shell 变量-#和*(继续)
一个可以接收多个命令行参数的安装程序的例子:
$ 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 命令给变量赋值是在程序执行之中,用户响应输入的提示而给变量赋值。
1.7 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 另外的技术
剩余21页未读,继续阅读
资源评论
wunesta
- 粉丝: 0
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功