Mailinglist Archive: opensuse (2496 mails)

< Previous Next >
Re: [opensuse] .bashrc alias - One Liner Problem?
  • From: "David C. Rankin" <drankinatty@xxxxxxxxxxxxxxxxxx>
  • Date: Mon, 01 Dec 2008 20:39:26 -0600
  • Message-id: <49349FDE.90809@xxxxxxxxxxxxxxxxxx>
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@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx

< Previous Next >