[opensuse] .bashrc alias - One Liner Problem?
Listmates, Usually I can make my 1-liner aliases work the way I want. However, I'm stumped by a simple webpin alias and its refusal to do what I think I'm telling it to do. Generally, I just use: alias wp='webpin $1' But I wanted to have it give usage information if no argument was given instead of just the default webpin "ERROR: you must specify the search criteria as a parameter" if no search term was specified. The simple logic I wanted was and that works fine in a separate script is: if [[ $1 ]]; then webpin $1 else echo -e "\n\tusage: wp <filename>\n" exit 1 fi However, when I try and turn it into a 1-liner I get the expected results for the echo statement, but I get the following error when webpin $1 is called: alias wp='if [[ $1 ]]; then webpin $1; else echo -e "\n\tusage: wp <filename>\n"; fi' 13:45 nirvana~/linux/scripts/test> wp usage: wp <filename> 13:49 nirvana~/linux/scripts/test> wp konqueror -bash: syntax error near unexpected token `konqueror' I look on both sides of the $1 in "webpin $1;" and I can't see any thing wrong. To get around this, I have tried various connotations of || and && logic, but I can't get either an exclusive execution of webpin or the usage statement, such as: alias wp='[[ $1 ]] && webpin $1 || echo -e "\n\tusage: wp <filename>\n"' Which works from the command line, but not as an alias. So my question is "what basic bash rule am I screwing up this time, and, how do I fix it?" Thanks. -- David C. Rankin, J.D.,P.E. | openSoftware und SystemEntwicklung Rankin Law Firm, PLLC | Countdown for openSuSE 11.1 www.rankinlawfirm.com | http://counter.opensuse.org/11.1/small -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
David C. Rankin wrote:
Listmates, <snip> So my question is "what basic bash rule am I screwing up this time, and, how do I fix it?" Thanks.
Yes, I know I can simply call the script from .bashrc and have it work, such as: /home/david/bin/wpsearch: #!/bin/bash if [[ $1 ]]; then webpin $1 else echo -e "\n\tusage: wp <filename>\n" exit 1 fi Then in .bashrc: alias wp='/home/david/bin/wpsearch $1' But, I was trying to find a way to do it all from .bashrc. I guess I could just make wpsearch a function in .bashrc, but that would violate the 1-line concept... -- David C. Rankin, J.D.,P.E. | openSoftware und SystemEntwicklung Rankin Law Firm, PLLC | Countdown for openSuSE 11.1 www.rankinlawfirm.com | http://counter.opensuse.org/11.1/small -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Sunday 30 November 2008 21:07:51 David C. Rankin wrote:
alias wp='if [[ $1 ]]; then webpin $1; else echo -e "\n\tusage: wp <filename>\n"; fi'
I don't think you can actually pass parameters to aliases. Your previous alias worked because wp expanded to "webpin $1" and $1 evaluated to the blank string You want to use a shell function instead. Something like function wp() { if [ -n "$1" ]; then webpin $1; else echo -e "\nbad usage\n"; fi } Anders -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Anders Johansson wrote:
On Sunday 30 November 2008 21:07:51 David C. Rankin wrote:
alias wp='if [[ $1 ]]; then webpin $1; else echo -e "\n\tusage: wp <filename>\n"; fi'
I don't think you can actually pass parameters to aliases. Your previous alias worked because wp expanded to "webpin $1" and $1 evaluated to the blank string
You want to use a shell function instead. Something like
function wp() { if [ -n "$1" ]; then webpin $1; else echo -e "\nbad usage\n"; fi }
Anders
Thank you all! I finally resolved to the function idea Aaron first suggested. wpsearch () { if [[ $1 ]]; then webpin $1 else echo -e "\n\tusage: wp <filename>\n" fi } alias wp='wpsearch $1' I guess I've been lucky using $1 with aliases. I don't know how they work, but they work in most instances for me: alias hist='history | grep $1' #Requires one input alias tmsg='sudo tail $1 -n50 /var/log/messages' # -f to follow alias tmi='sudo tail $1 -n50 /var/log/mail.info' # -f to follow alias umnt='/home/david/bin/alias_umnt $1' alias wg='[[ -n $1 ]] || echo -e "\n\tUsage: wg <filename>\t\t(runs wget --no-check-certificate --progress=bar)\n"; wget --no-check-certificate --progress=bar $1' #requires input alias zu='/home/david/linux/scripts/config/zypp/zypp_auto $1' # either up for patches or -t packages for pagkages alias znr='sudo zypper nr $1 $2' # requires two inputs <old repo alias> <new repo alias> That's why I was surprised to be having trouble with the webpin alias. If I recall correctly, I was actually introduced to the command line parameter in aliases idea through a BASH-howto. -- David C. Rankin, J.D.,P.E. | openSoftware und SystemEntwicklung Rankin Law Firm, PLLC | Countdown for openSuSE 11.1 www.rankinlawfirm.com | http://counter.opensuse.org/11.1/small -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Monday 01 December 2008 19:12:24 David C. Rankin wrote:
I finally resolved to the function idea Aaron first suggested.
Who is Aaron?
wpsearch () { if [[ $1 ]]; then webpin $1 else echo -e "\n\tusage: wp <filename>\n" fi }
alias wp='wpsearch $1'
This is pointless. "wp konqueror" expands to wpsearch $1 konqueror and the only reason your alias works is that this is a valid string, since $1 is the empty string after expansion. But try starting this from a script where you pass parameters. Then $1 won't be empty, and then you will get weird results
I guess I've been lucky using $1 with aliases. I don't know how they work, but they work in most instances for me:
alias hist='history | grep $1' #Requires one input alias tmsg='sudo tail $1 -n50 /var/log/messages' # -f to follow alias tmi='sudo tail $1 -n50 /var/log/mail.info' # -f to follow alias umnt='/home/david/bin/alias_umnt $1' alias wg='[[ -n $1 ]] || echo -e "\n\tUsage: wg <filename>\t\t(runs wget --no-check-certificate --progress=bar)\n"; wget --no-check-certificate --progress=bar $1' #requires input alias zu='/home/david/linux/scripts/config/zypp/zypp_auto $1' # either up for patches or -t packages for pagkages alias znr='sudo zypper nr $1 $2' # requires two inputs <old repo alias> <new repo alias>
Same thing for all of these. "hist konqueror" expands to history | grep $1 konqueror which works because $1 is the empty string. tmsg -f expands to sudo tail $1 -n 50 /var/log/messages -f which again is a valid command line And so on and so on and so forth Anders -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Anders Johansson wrote:
On Monday 01 December 2008 19:12:24 David C. Rankin wrote:
I finally resolved to the function idea Aaron first suggested.
Who is Aaron?
Kulkis
This is pointless. "wp konqueror" expands to
wpsearch $1 konqueror
and the only reason your alias works is that this is a valid string, since $1 is the empty string after expansion. But try starting this from a script where you pass parameters. Then $1 won't be empty, and then you will get weird results
I guess I've been lucky using $1 with aliases. I don't know how they work, but they work in most instances for me:
alias hist='history | grep $1' #Requires one input
Same thing for all of these. "hist konqueror" expands to
history | grep $1 konqueror
which works because $1 is the empty string. tmsg -f expands to
sudo tail $1 -n 50 /var/log/messages -f
which again is a valid command line
And so on and so on and so forth
Anders
Thanks Anders, Lucky was the operative word. I will find whatever howto suggested this in the first place and convey the information. I think I still have the link somewhere. Further experimentation shows, as always, you are dead-on: alias tstinput='echo -e "\ntest\n\$2=$2; \n\$1=$1; \n\$3=$3\n"' . .bashrc 13:19 alchemy~> tstinput one two three test $2=; $1=; $3= one two three Mystery solved. -- David C. Rankin, J.D.,P.E. | openSoftware und SystemEntwicklung Rankin Law Firm, PLLC | Countdown for openSuSE 11.1 www.rankinlawfirm.com | http://counter.opensuse.org/11.1/small -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
----- Original Message ----- From: "David C. Rankin" <drankinatty@suddenlinkmail.com> To: "suse" <opensuse@opensuse.org> Sent: Monday, December 01, 2008 2:23 PM Subject: Re: [opensuse] .bashrc alias - One Liner Problem?
Anders Johansson wrote:
On Monday 01 December 2008 19:12:24 David C. Rankin wrote:
I finally resolved to the function idea Aaron first suggested.
Who is Aaron?
Kulkis
This is pointless. "wp konqueror" expands to
wpsearch $1 konqueror
and the only reason your alias works is that this is a valid string, since $1 is the empty string after expansion. But try starting this from a script where you pass parameters. Then $1 won't be empty, and then you will get weird results
I guess I've been lucky using $1 with aliases. I don't know how they work, but they work in most instances for me:
alias hist='history | grep $1' #Requires one input
Same thing for all of these. "hist konqueror" expands to
history | grep $1 konqueror
which works because $1 is the empty string. tmsg -f expands to
sudo tail $1 -n 50 /var/log/messages -f
which again is a valid command line
And so on and so on and so forth
Anders
Thanks Anders,
Lucky was the operative word. I will find whatever howto suggested this in the first place and convey the information. I think I still have the link somewhere. Further experimentation shows, as always, you are dead-on:
alias tstinput='echo -e "\ntest\n\$2=$2; \n\$1=$1; \n\$3=$3\n"' . .bashrc 13:19 alchemy~> tstinput one two three
test $2=; $1=; $3= one two three
Mystery solved.
The how-to may not necessarily be wrong. einstein was right, everything is relative, and what is untrue in one context may be true in some other. If you had a script, you could put som aliases in the script and, depending on your quoting/escaping syntax, you could put $1,$2 etc in the alias. What would happen is a one-time fixed expansion would happen where the args given on the script command line would get expaded and inserted into the alias. So if you have a script named myscript like this: ---TOF--- #!/bin/sh alias show1="echo arg1 is $1" alias show2="echo arg2 is $2" show1 show2 ---EOF--- Notice the alias is not single-quoted, and so the $1 gets expanded immediately at the time the alias definition is read and assigned. And the you ran the script like this: myscript hello there the output would be arg1 is hello arg2 is there if the script had these lines instead: show1 foo bar show2 foo bar the output would be: arg1 is hello foo bar arg2 is there foo bar The foo bar are not being passe as arguments to the alias. Rather the alias is expanding in-place right there on that line as if it were a variable. so at the time the alias is defined, alias show1="echo arg1 is $1" becomes alias show1="echo arg1 is hello" then later when you use it, show1 foo bar becomes echo arg1 is hello foo bar and then this echo command is run. Now, If you single-quote the alias, you get a slightly different result. Then the $1 is not expanded at the time the alias is defined, but it will be expanded later at run-time, but it will end up expanding to the same value, which is $1 to the script, not what looks like arguments passed to the alias. The end result is the same (so far) but notice the different order that things happen: at the time the alias is defined alias show1='echo arg1 is $1' the $1 stays a litteral $1 so then at run-time: show1 foo bar becomes echo arg1 is $1 foo bar which then becomes echo arg1 is hello foo bar Now the part that might cause confusion if you don't understand the various orders of operation and nesting and quoting etc... If you have the alias, which includes $1, inside a script, and it's single-quoted, Then it starts out producing the same result as the first example, just expanding to the scripts original command line arguments. But, since the $1 is expanding at the last minute, then it will reflect changes to the current scripts argv as a result of the shift and set commands. so take this script: ---TOF--- #!/bin/sh alias show1='echo arg1 is $1' alias show2='echo arg2 is $2' show1 show2 echo using shift once shift show1 show2 echo using set to set a new argv set holy cow show1 show2 echo adding foo bar at run-time show1 foo bar show2 foo bar ---EOF--- You get the following output: whoa:~ $ ./myscript hello there arg1 is hello arg2 is there using shift once arg1 is there arg2 is using set to set a new argv arg1 is holy arg2 is cow adding foo bar at run-time arg1 is holy foo bar arg2 is cow foo bar whoa:~ $ Normally in .basrc there is no argv because no command line args are passed to the initial interactive shell. But, if .bashrc ever gets invoked at any other times, maybe then sometimes there are command line arguments. So it's POSSIBLE that there is some use for aliases that include $1,$2,etc in .bashrc , but I don't know any off hand. So depending on what exactly the how-to was about, and what exact context it was talking about, it wasn't automatically wrong just because it involved an alias that uncludes a $1 -- Brian K. White brian@aljex.com http://www.myspace.com/KEYofR +++++[>+++[>+++++>+++++++<<-]<-]>>+.>.+++++.+++++++.-.[>+<---]>++. filePro BBx Linux SCO FreeBSD #callahans Satriani Filk! -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Monday 01 December 2008 10:17, Anders Johansson wrote:
On Monday 01 December 2008 19:12:24 David C. Rankin wrote:
I finally resolved to the function idea Aaron first suggested.
Who is Aaron?
That's not funny. Poor exiled Aaron has been relegated to watching from outside and sending direct replies to posters when he has answers for them. Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Monday 01 December 2008 20:29:51 Randall R Schulz wrote:
On Monday 01 December 2008 10:17, Anders Johansson wrote:
On Monday 01 December 2008 19:12:24 David C. Rankin wrote:
I finally resolved to the function idea Aaron first suggested.
Who is Aaron?
That's not funny. Poor exiled Aaron has been relegated to watching from outside and sending direct replies to posters when he has answers for them.
I actually thought it was a very weird misspelling of my name, since I suggested that function (or one like it - I don't see the point of using [[ ]]). I've seen some extremely strange versions of my name, so it wouldn't be the first time I have nothing against Aaron, and if he can get along without starting flamewars, I'm all for giving him another chance Anders -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Anders Johansson wrote:
On Monday 01 December 2008 20:29:51 Randall R Schulz wrote:
On Monday 01 December 2008 19:12:24 David C. Rankin wrote:
I finally resolved to the function idea Aaron first suggested. Who is Aaron? That's not funny. Poor exiled Aaron has been relegated to watching from outside and sending direct replies to posters when he has answers for
On Monday 01 December 2008 10:17, Anders Johansson wrote: them.
I actually thought it was a very weird misspelling of my name, since I suggested that function (or one like it - I don't see the point of using [[ ]]). I've seen some extremely strange versions of my name, so it wouldn't be the first time
I have nothing against Aaron, and if he can get along without starting flamewars, I'm all for giving him another chance
Anders
I feel the same... His help is great, and even in his less diplomatic moments, his arguments are normally correct, just a bit singed around the edges from the adjoining hyperbole. -- David C. Rankin, J.D.,P.E. | openSoftware und SystemEntwicklung Rankin Law Firm, PLLC | Countdown for openSuSE 11.1 www.rankinlawfirm.com | http://counter.opensuse.org/11.1/small -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Hello, On Sun, 30 Nov 2008, David C. Rankin wrote:
alias wp='webpin $1'
That does not work as you think. Aliases are substituted, not called with arguments. ==== man bash ==== ALIASES Aliases allow a string to be substituted for a word when it is used as the first word of a simple command. ====
But I wanted to have it give usage information if no argument was given instead of just the default webpin "ERROR: you must specify the search criteria as a parameter" if no search term was specified.
You can't.
The simple logic I wanted was and that works fine in a separate script is:
if [[ $1 ]]; then webpin $1 else echo -e "\n\tusage: wp <filename>\n" exit 1 fi
A script get's passed arguments.
However, when I try and turn it into a 1-liner I get the expected results for the echo statement, but I get the following error when webpin $1 is called:
alias wp='if [[ $1 ]]; then webpin $1; else echo -e "\n\tusage: wp <filename>\n"; fi'
Note what happens if I add a ';' at the end: $ alias foo='if [[ $1 ]]; then webpin $1; else echo -e "\tusage: p <filename>"; fi;' $ foo a b c $ foo a b c + [[ -n '' ]] + echo -e '\tusage: wp <filename>' usage: wp <filename> + a b c bash: a: command not found Which is because foo a b c is substituted to: if [[ $1 ]]; then ... fi; a b c (and "$1" expands to nothing).
So my question is "what basic bash rule am I screwing up this time, and, how do I fix it?" Thanks.
See above. Use a function: wp() { if test -n "$1"; then webpin "$@"; else echo -e "..."; fi; } Passing "$@" instead of just "$1" to webpin makes it more flexible as you can pass options to wepin, e.g. "wp -d latest konqueror" HTH, -dnh --
This needs quotes: use lib "/path/to/perl/modules"; Single or double quotes? Yes. -- Tad McClellan in comp.lang.perl.misc -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
David Haller wrote:
Hello,
On Sun, 30 Nov 2008, David C. Rankin wrote:
alias wp='webpin $1'
That does not work as you think.
Aliases are substituted, not called with arguments.
==== man bash ==== ALIASES Aliases allow a string to be substituted for a word when it is used as the first word of a simple command. ====
But I wanted to have it give usage information if no argument was given instead of just the default webpin "ERROR: you must specify the search criteria as a parameter" if no search term was specified.
You can't.
The simple logic I wanted was and that works fine in a separate script is:
if [[ $1 ]]; then webpin $1 else echo -e "\n\tusage: wp <filename>\n" exit 1 fi
A script get's passed arguments.
However, when I try and turn it into a 1-liner I get the expected results for the echo statement, but I get the following error when webpin $1 is called:
alias wp='if [[ $1 ]]; then webpin $1; else echo -e "\n\tusage: wp <filename>\n"; fi'
Note what happens if I add a ';' at the end:
$ alias foo='if [[ $1 ]]; then webpin $1; else echo -e "\tusage: p <filename>"; fi;'
$ foo a b c $ foo a b c + [[ -n '' ]] + echo -e '\tusage: wp <filename>' usage: wp <filename> + a b c bash: a: command not found
Which is because foo a b c is substituted to:
if [[ $1 ]]; then ... fi; a b c
(and "$1" expands to nothing).
So my question is "what basic bash rule am I screwing up this time, and, how do I fix it?" Thanks.
See above. Use a function:
wp() { if test -n "$1"; then webpin "$@"; else echo -e "..."; fi; }
Passing "$@" instead of just "$1" to webpin makes it more flexible as you can pass options to wepin, e.g. "wp -d latest konqueror"
HTH, -dnh
Always David. That is a great explanation. Thanks. -- David C. Rankin, J.D.,P.E. | openSoftware und SystemEntwicklung Rankin Law Firm, PLLC | Countdown for openSuSE 11.1 www.rankinlawfirm.com | http://counter.opensuse.org/11.1/small -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (5)
-
Anders Johansson
-
Brian K. White
-
David C. Rankin
-
David Haller
-
Randall R Schulz