[opensuse] alias failure
I'm having no luck figuring out why alias Vol='tune2fs -l $1 | grep volume' 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? -- "All scripture is God-breathed and is useful for teaching, rebuking, correcting, and training in righteoousness." 2 Timothy 3:16 NIV Team OS/2 ** Reg. Linux User #211409 Felix Miata *** http://mrmazda.no-ip.com/ -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Felix Miata wrote:
I'm having no luck figuring out why
alias Vol='tune2fs -l $1 | grep volume'
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?
That works fine here - I'd be curious to know more about /dev/hda7 Joe -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On 2007/07/28 13:42 (GMT-0700) joe apparently typed:
Felix Miata wrote:
I'm having no luck figuring out why
alias Vol='tune2fs -l $1 | grep volume'
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?
That works fine here
It gets me exactly what I want from the command line, but fails as an alias.
- I'd be curious to know more about /dev/hda7
Leave off the grep filter? When I want to know if there is a volume label set, I run tune2fs -l on the appropriate partition. Without a grep or less filter, the only part of its output that I want scrolls offscreen before I can see it. -- "All scripture is God-breathed and is useful for teaching, rebuking, correcting, and training in righteoousness." 2 Timothy 3:16 NIV Team OS/2 ** Reg. Linux User #211409 Felix Miata *** http://mrmazda.no-ip.com/ -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
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 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 What you're doing is tryting to run grep on /dev/hda7. Let's hope you don't have read access!
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 } (If you put that all on one line, you'll need a semicolon after "volume" and before the closing brace.) Beware that if you're going to try this, you should undefine the alias first. They intefere, and if I'm not mistaken the alias will override the shell procedure. Once you get something you like, put it in your .bashrc, though realistically, there's no particular reason not to just make a shell script out of this. 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.
"All scripture is God-breathed and is useful for teaching, rebuking, correcting, and training in righteoousness." 2 Timothy 3:16 NIV
And still I help you. Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Randall R Schulz wrote:
Aliases don't take positional parameters, at least not in BASH (I think 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:
<Smacks forehead> Yes, Randall is right. I took a lazy shortcut and did this instead: for i in `cat drives`; do tune2fs -l $i | grep volume; done which of course worked.... Joe -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On 2007/07/28 14:46 (GMT-0700) joe apparently typed:
Randall R Schulz wrote:
Aliases don't take positional parameters, at least not in BASH (I think 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:
Yes, Randall is right. I took a lazy shortcut and did this instead:
for i in `cat drives`; do tune2fs -l $i | grep volume; done
which of course worked....
Not for me. I put that in a script, and got 'cat: drives: No such file or directory', and get a syntax error unexpected token from an alias. :-( -- "All scripture is God-breathed and is useful for teaching, rebuking, correcting, and training in righteoousness." 2 Timothy 3:16 NIV Team OS/2 ** Reg. Linux User #211409 Felix Miata *** http://mrmazda.no-ip.com/ -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Felix Miata wrote:
On 2007/07/28 14:46 (GMT-0700) joe apparently typed:
Randall R Schulz wrote:
Aliases don't take positional parameters, at least not in BASH (I think 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:
Yes, Randall is right. I took a lazy shortcut and did this instead:
for i in `cat drives`; do tune2fs -l $i | grep volume; done
which of course worked....
Not for me. I put that in a script, and got 'cat: drives: No such file or directory', and get a syntax error unexpected token from an alias. :-(
Well, you'd have to have a file called "drives" containing the partitions you want to run against... in my case it was rather simple-minded: root@libby:~> cat drives /dev/hda1 root@libby:~> Joe -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
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'
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.
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.
(If you put that all on one line, you'll need a semicolon after "volume" and before the closing brace.)
Beware that if you're going to try this, you should undefine the alias first. They intefere, and if I'm not mistaken the alias will override the shell procedure.
Once you get something you like, put it in your .bashrc, though realistically, there's no particular reason not to just make a shell script out of this.
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).
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. :-( -- "All scripture is God-breathed and is useful for teaching, rebuking, correcting, and training in righteoousness." 2 Timothy 3:16 NIV Team OS/2 ** Reg. Linux User #211409 Felix Miata *** http://mrmazda.no-ip.com/ -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
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: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Sunday 29 July 2007 03:32, Felix Miata wrote:
FM I'm having no luck figuring out why FM alias Vol='tune2fs -l $1 | grep volume' FM FM causes a usage message when 'Vol /dev/hda7' is run. Can anyone explain what FM I'm doing wrong, or provide a better method to discover a volume label?
I remember that $1 - is the first argument in bash scripts, but I don't remember how $1 works in command line. And I think it will be a good idea to try this: ===script====== #!/bin/bash tune2fs -l $1 | grep volume ===end script=== -- WBR, Dmitry. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Saturday 28 July 2007 23:57, Dmitry wrote:
On Sunday 29 July 2007 03:32, Felix Miata wrote:
FM I'm having no luck figuring out why FM alias Vol='tune2fs -l $1 | grep volume' FM FM causes a usage message when 'Vol /dev/hda7' is run. Can anyone explain what FM I'm doing wrong, or provide a better method to discover a volume label?
I remember that $1 - is the first argument in bash scripts, but I don't remember how $1 works in command line. And I think it will be a good idea to try this:
By "command line," I take it you mean when the shell is interactive. The answer is essentially the same, a positional parameter or all parameters (for $* or $@) are substituted in place of the variable reference. Interactive shells usually don't have positional parameters, but they can. Just as with any other shell, they can be passed when the shell is launched or can be established (and changed) later using the "set" built-in. We've been here already:
===script====== #!/bin/bash tune2fs -l $1 | grep volume ===end script===
-- WBR, Dmitry.
Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Dmitry wrote:
On Sunday 29 July 2007 03:32, Felix Miata wrote:
FM I'm having no luck figuring out why FM alias Vol='tune2fs -l $1 | grep volume' FM FM causes a usage message when 'Vol /dev/hda7' is run. Can anyone explain what FM I'm doing wrong, or provide a better method to discover a volume label?
I remember that $1 - is the first argument in bash scripts, but I don't remember how $1 works in command line. And I think it will be a good idea to try this:
Actually, $0 is the first, which contains the command used to call it. Then each additional item is the additional parameters from the command. So, if you entered "myscript a 1 here, the parameters would be: $0 myscript $1 a $2 1 $3 here You might wonder about the usefulness of $0, but remember that you can have multiple links to the same script, but with different names. Then you could use that name to determine action within the script. -- Use OpenOffice.org <http://www.openoffice.org> -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Sunday 29 July 2007 23:42, James Knott wrote:
JK >> FM I'm having no luck figuring out why JK >> FM alias Vol='tune2fs -l $1 | grep volume' JK >> FM JK >> FM causes a usage message when 'Vol /dev/hda7' is run. Can anyone explain JK FM>> what FM I'm doing wrong, or provide a better method to discover a volume JK FM>> label?
JK > I remember that $1 - is the first argument in bash scripts, but I don't JK > remember how $1 works in command line. And I think it will be a good idea to JK > try this: <skipped> JK Actually, $0 is the first, which contains the command used to call it. <skipped>
I mean that script works , but when we use pipelines is `alias` we can get unusual things. For example :
alias test='echo $1 | grep qwerty; echo $1'; test qwertyu qwertyu
But if we write this code in script we'll get qwertyu qwertyu Thats why I use aliases only on simple commands and scripts on others. -- WBR, Dmitry. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Sunday 29 July 2007 11:43, Dmitry wrote:
... For example :
alias test='echo $1 | grep qwerty; echo $1';
This is never a sensible alias. I'll say it again, positional parameters in aliases are not what they seem to be. They are not substituted with the parameters used when the alias is invoked, they are the parameters passed (or established within) the shell executing the alias.
test qwertyu
This is equivalent to issuing this command: % echo $1 | grep qwert; echo $1 qwerty
qwertyu
You get this results _only because the shell you're using has no positional parameters_. Try this with your alias (by the way, calling it "test" is a very bad idea, since you usurp the test built-in and executable by doing so): % set argumentTheFirst % test qwerty What do you think you'll see? It is: argumentTheFirst qwerty
But if we write this code in script we'll get qwertyu qwertyu
That's because putting the body of the alias in a script is nothing like that alias.
Thats why I use aliases only on simple commands and scripts on others.
Aliases never were meant to handle positional parameter substitution. It's critical to know this or you'll continue to be frustrated and confused by their behavior.
-- WBR, Dmitry.
Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (5)
-
Dmitry
-
Felix Miata
-
James Knott
-
joe
-
Randall R Schulz