On Saturday 28 July 2007 16:39, Felix Miata wrote:
> On 2007/07/28 13:58 (GMT-0700) Randall R Schulz apparently typed:
> > On Saturday 28 July 2007 13:32, Felix Miata wrote:
> >> I'm having no luck figuring out why
> >>
> >> alias Vol='tune2fs -l $1 | grep volume'
> >
> > Aliases don't take positional parameters, at least not in BASH (I
> > think
>
> So it's just an accident that the following aliases all work as I
> want/expect? alias ll='ls -l $*'
> alias rpmqa='rpm -qa | grep $*'
> alias test='echo $*'
> alias vol='tune2fs -l $1'
In all cases, your $1 or $* are at the end, so yes, it is just a
coincidence. (And that's the reason your Vol alias didn't work—you used
the positional parameter in the middle, and that's why it didn't do
what you expected.)
It's uncommon for interactive shells to have any positional parameters
(they're usually invoked with options only), so those references to
positional parameters end up doing nothing. But if your interactive
shell had positional parameters (either from its invocation, however
unlikely that is, or through use of the "set" built-in), you would have
seen them (or the first of them) being passed to the various commands
ahead of the arguments you gave when invoking those aliases.
> > they do in the Csh family, if I recall correctly). They simply
> > expanded verbatim in front of any arguments you give, so if you
> > invoke it with "/dev/hda7" as an argument, it's like running this
> > command:
> >
> > % tune2fs -l $1 | grep volume /dev/hda7
>
> It's still clear as mud how "don't take positional parameters"
> translates into moving /dev/hda7 to the end of the whole string.
>
> > What you're doing is tryting to run grep on /dev/hda7. Let's hope
> > you don't have read access!
>
> I see what you wrote, but don't understand how /dev/hda7 shows up at
> the end of everything.
Because the when the command is an alias, the alias is expanded
literally—without any alteration—in place of the alias name. Once
that's done, interpretation of the command line continues as with other
command, with any arguments you may have supplied implicitly ending up
after all the contents of the alias definition.
> >> causes a usage message when 'Vol /dev/hda7' is run. Can anyone
> >> explain what I'm doing wrong, or provide a better method to
> >> discover a volume label? --
> >
> > Unlike the very limited capabilities of aliases, shell procedures
> > are just like separate scripts, except no file need be loaded to
> > invoke them. You can get the effect I think you want with this:
> >
> > Vol() {
> > tune2fs -l "$1" |grep volume
> > }
>
> I made a script with nothing but that in it, but it returns nothing.
That syntax _defines_ a shell procedure but does not invoke it. If you
just put it in a script and invoke the script, it's rather like a
script that just sets variables. The variables get set then the shell
interpreting the script exits and nothing of consequence happens.
If you want to use this as a plain script, make a file containing this:
-==--==--==--==--==--==--==--==--==--==-
#!/bin/bash --norc
tune2fs -l "$1" |grep volume
-==--==--==--==--==--==--==--==--==--==-
Alternately (and I show this just for pedagogical reasons), you could
take the non-functioning script you created:
-==--==--==--==--==--==--==--==--==--==-
Vol() {
tune2fs -l "$1" |grep volume
}
-==--==--==--==--==--==--==--==--==--==-
and modify it thusly (the #! line with the --norc option is always a
good idea):
-==--==--==--==--==--==--==--==--==--==-
#!/bin/bash --norc
Vol() {
tune2fs -l "$1" |grep volume
}
Vol "$1"
-==--==--==--==--==--==--==--==--==--==-
You'll get the same effect as the first script I showed above
> ...
>
> Other than the quotes, I don't see the difference between the content
> of your sample script, and putting essentially the same thing into
> .bashrc, which is where all my aliases live, and why I use aliases
> instead of simple scripts (easier to copy one file to new username on
> new installation).
If you put it in .bashrc (in the form I originally showed), every shell
you launch will have a command (in the form of a shell procedure)
called "Vol" available and no path searching or file loading will have
to take place in order to run it when it's mentioned in a script or
interactive session.
If you create a (proper form of) the script somewhere in your PATH, then
it will be executed as any other script (via path searching and by
asking the kernel to execute that script).
> > Lastly, don't use an "exit" for early return in a shell procedure.
> > It will apply to the shell that invoked it. There's a "return"
> > keyword that works the same as exit and causes just the shell
> > procedure to terminate before reaching its last statement, not the
> > whole shell.
>
> I appreciate the reply, but I'm not sure I understand any more now
> than I did before starting the thread. :-(
How about now?
How much do I have to help you before you'll stop proselytizing here?
Randall Schulz
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]