Chinaunix首页 | 论坛 | 博客
  • 博客访问: 146945
  • 博文数量: 52
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 490
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-05 12:05
文章分类

全部博文(52)

文章存档

2013年(1)

2010年(3)

2009年(6)

2008年(25)

2007年(17)

我的朋友

分类:

2008-10-13 11:33:00

The Bourne shell command line can have options like -e (exit if any command returns non-zero status). It can also have other arguments; these are passed to shell scripts. You can set new command-line parameters while you're typing interactive commands (at a shell prompt) or in a shell script.

To reset the command-line parameters, just type set followed by the new parameters. So, for example, to ask the shell to show expanded versions of command lines after you type them, set the -v (verbose) option :

$ set -v
$ mail $group1 < message
mail andy ellen heather steve wilma < message
$ mail $group2 < message
mail jpeek@jpeek.com randy@xyz.edu yori@mongo.medfly.com < message
$ set +v

Typing set +v cancels the v option on many Bourne shells.

You can put filenames or any other strings in the command-line parameters interactively or from a shell script. That's handy for storing and parsing the output of a UNIX command with backquotes . For example, you can get a list of all logged-in users from the parameters $1, $2, and so on. Use users if your system has it. Otherwise, use who - and cut  to strip off everything but the usernames:





$ set `users`
$ set `who | cut -c1-8`
$ for u
> do
> ...do something with each user ($u)...
> done

You can save the original parameters in another variable and reset them later:

oldparms="$*"
set something new
...use new settings...
set $oldparms

If the first parameter you set starts with a dash, like -e, the shell will treat it as its own option instead of as a string to put into the command-line parameters. To avoid this, use -- (two dashes) as the first argument to set. In this example, $1 gets -e, and the filenames expanded from the wildcard pattern go into $2, $3, etc.:

set -- -e file*

Because the shell parses and scans the new parameters before it stores them, wildcards  and other special characters will be interpreted - watch your quoting . You can take advantage of this to parse lines of text into pieces that aren't separated with the usual spaces and TABs - for instance, a line from a database with colon-separated fields - by setting the IFS  variable before the set command.

If you want to save any special quoting on the original command line, be careful; the quoting will be lost unless you're clever. For example, if $1 used to be John Smith, it'll be split after it's restored: $1 will have John and $2 will be Smith. A better solution might be to use a subshell for the part of the script where you need to reset the command-line parameters:

# reset command-line parameters during subshell only:
(set some new parameters
...do something with new parameters...
)
# original parameters aren't affected from here on...

One last note: set won't set $0, the name of the script file.

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