Chinaunix首页 | 论坛 | 博客
  • 博客访问: 92284650
  • 博文数量: 19283
  • 博客积分: 9968
  • 博客等级: 上将
  • 技术积分: 196062
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-07 14:28
文章分类

全部博文(19283)

文章存档

2011年(1)

2009年(125)

2008年(19094)

2007年(63)

分类:

2008-04-18 23:06:12

作者:hrbpost  
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

h: 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

hift 2

echo There are $# 

comand line arguments

echo They are $*

echo Shifting two arguments

hift 2; final_args=$*

echo Original arguments are: $orig_args

echo Final arguments are: $final_args


hift命令会重新分配命令行参数对应位置参数,在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 另外的技术

#号开始的文档为注释部分。

h shell_program argumetns 

hell_program 的属性可以不是可执行的。

hell_program 必须是可读的。

h –x shell_program arguments

每一行在被执行前被打印出来在调试程序时有用处。

在shell程序中,#符号被用来提供一段注释。shell会忽略#符号后边的字符,直到一个回车符号为止。 执行一个shell程序的另外一种方法是:sh shell_program arguments

这种方式激活一个子shell并且指定这个子shell为执行这个程序的命令解释器。这个程序文件不是必须为可执行的。这种方式的用途在:你正在在一种shell下工作,同时想要执行用其他shell命令语言写的shell程序十分有用。

你也可以在你的shell程序的第一行前加入#!/usr/bin/ shell_name来指定命令行解释器。因此,如果你当前正在POSIX shell下工作,但是想要执行一个C shell的脚本,你的C shell程序的第一行应该为:

#!/usr/bin/csh



虽然shell程序没有调试器,命令:

h –x shell_program arguments



会在执行每一行时,先在屏幕上打印出shell程序的每一行。这允许你看到shell如何进行文件名产生,变量替代,和命令替代。这个选项对发现打字错误十分有帮助。

阅读(280) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~