分类:
2008-10-13 11:33:00
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:
$ |
You can save the original parameters in another variable and reset them later:
oldparms="$*"
setsomething 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:
(setsome 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.