Re: Issues when func name is the same with an alias

2010-08-06 Thread Marc Herbert
This is much more code:

 F(){ find $@; }

 This simple func definition will be 4 lines in my coding style. :) And I
 like adding at least one blank line between functions.

Then just exclude .bashrc from your coding style.




Re: Issues when func name is the same with an alias

2010-08-05 Thread Marc Herbert
Le 04/08/2010 15:29, Clark J. Wang a écrit :
 I do not agree. Aliases are much simpler to use than functions.

Please provide examples.





Re: Issues when func name is the same with an alias

2010-08-05 Thread Clark J. Wang
On Thu, Aug 5, 2010 at 4:06 PM, Marc Herbert marc.herb...@gmail.com wrote:

 Le 04/08/2010 15:29, Clark J. Wang a écrit :
  I do not agree. Aliases are much simpler to use than functions.

 Please provide examples.

 The following is a part of my aliases. I'll have to write much more code if
I define them all as functions. I don't think functions are better than
aliases here. Any idea?

A='alias | grep ^alias .= | sed -e s/^alias // | LC_ALL=C command grep
^[a-zA-Z]'
B='builtin'
C='command'
D='date +%a %Y-%m-%d %H:%M:%S (%z %Z)'
E='export'
F='find'
G='git'
H='history'
I='ifconfig'
K='killall'
L='less'
M='LC_ALL=C man'
N='netstat'
O='openssl'
P='popd'
Q='[[ $WINDOW ]]  screen -X at '\''#'\'' stuff $'\''exit\n'\'''
R='route'
S='sudo'
T='touch'
U='uname'
V='echo $BASH_VERSION'
W='watch'
X='screen -X'
Y='yast2'
Z='reset'
a='alias'
b='builtin cd -'
c='clear'
d='dirs -v'
e='echo'
f='file'
g='grep'
h='help'
i='info --vi-keys'
j='jobs'
k='kill'
l='ls -l'
m='make'
n='mount'
p='pushd'
q='exit'
r='fc -s'
s='screen'
t='type'
u='umount'
v='vim'
w='w'
x='xargs'
y='yast'
z='echo ${COLUMNS}x$LINES'


Re: Issues when func name is the same with an alias

2010-08-05 Thread Chris F.A. Johnson

On Fri, 6 Aug 2010, Clark J. Wang wrote:


On Thu, Aug 5, 2010 at 4:06 PM, Marc Herbert marc.herb...@gmail.com wrote:


Le 04/08/2010 15:29, Clark J. Wang a écrit :

I do not agree. Aliases are much simpler to use than functions.


Please provide examples.

The following is a part of my aliases. I'll have to write much more code if

I define them all as functions.


This is much more code:

F(){ find $@; }

than:

alias F=find


I don't think functions are better than aliases here. Any idea?


Many reasons why functions are generally better have already been
given.

--
   Chris F.A. Johnson, http://cfajohnson.com
   Author:
   Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)

Re: Issues when func name is the same with an alias

2010-08-05 Thread Clark J. Wang
On Fri, Aug 6, 2010 at 9:54 AM, Chris F.A. Johnson ch...@cfajohnson.comwrote:

 On Fri, 6 Aug 2010, Clark J. Wang wrote:

  On Thu, Aug 5, 2010 at 4:06 PM, Marc Herbert marc.herb...@gmail.com
 wrote:

  Le 04/08/2010 15:29, Clark J. Wang a écrit :

 I do not agree. Aliases are much simpler to use than functions.


 Please provide examples.

 The following is a part of my aliases. I'll have to write much more code
 if

 I define them all as functions.


This is much more code:

 F(){ find $@; }

 This simple func definition will be 4 lines in my coding style. :) And I
like adding at least one blank line between functions.


than:

 alias F=find


  I don't think functions are better than aliases here. Any idea?


Many reasons why functions are generally better have already been
given.

 I understand functions are *generally* better and there're quite a lot of
functions defined in my bashrc.  I just don't agree we have to avoid using
aliases.

  --
   Chris F.A. Johnson, http://cfajohnson.com
   Author:
   Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)


Re: Issues when func name is the same with an alias

2010-08-04 Thread Marc Herbert
Le 04/08/2010 11:39, Clark J. Wang a écrit :

 Seems like I must explicitly use the `function' keyword to define foo() for
 this scenario. Is that the correct behavior?

The correct behaviour is simply not to use aliases, since they bring nothing
to the table compared to functions. Have a look at this:
http://thread.gmane.org/gmane.comp.shells.bash.bugs/13865/focus=13901


About the function keyword have a look at the discussion two days ago.





Re: Issues when func name is the same with an alias

2010-08-04 Thread Bernd Eggink

Am 04.08.2010 12:39, schrieb Clark J. Wang:

I was testing the precedence between functions and aliases so I tried like
this (with bash 4.1.5):

$ cat rc
alias foo='echo this is the alias'

foo()
{
 builtin echo 'this is the function'
}

foo
$ source rc
bash: confusing-aliases-2.sh: line 4: syntax error near unexpected token `('
bash: confusing-aliases-2.sh: line 4: `foo()'
$

Seems like I must explicitly use the `function' keyword to define foo() for
this scenario. Is that the correct behavior?


The man page says The first word of a simple command, if unquoted, is 
checked to see if has an alias. Therefore 'foo' in your function 
declaration is replaced by 'echo this is the alias'. Unfortunately, you 
can't quote the function name in the declaration, so you have to either 
use 'function' or say unalias foo first.


Regards,
Bernd

--
Bernd Eggink
http://sudrala.de



Re: Issues when func name is the same with an alias

2010-08-04 Thread Eric Blake
On 08/04/2010 05:03 AM, Marc Herbert wrote:
 Le 04/08/2010 11:39, Clark J. Wang a écrit :
 
 Seems like I must explicitly use the `function' keyword to define foo() for
 this scenario. Is that the correct behavior?
 
 The correct behaviour is simply not to use aliases, since they bring nothing
 to the table compared to functions.

Not _quite_ true - there are a few things aliases can do that functions
cannot, and when combined, you can get some cool interactive effects
(although I don't recommend relying on this in scripts):
http://www.chiark.greenend.org.uk/~sgtatham/aliases.html

-- 
Eric Blake   ebl...@redhat.com+1-801-349-2682
Libvirt virtualization library http://libvirt.org



signature.asc
Description: OpenPGP digital signature


Re: Issues when func name is the same with an alias

2010-08-04 Thread Bernd Eggink

Am 04.08.2010 15:13, schrieb Eric Blake:

On 08/04/2010 05:03 AM, Marc Herbert wrote:

Le 04/08/2010 11:39, Clark J. Wang a écrit :


Seems like I must explicitly use the `function' keyword to define foo() for
this scenario. Is that the correct behavior?


The correct behaviour is simply not to use aliases, since they bring nothing
to the table compared to functions.


Not _quite_ true - there are a few things aliases can do that functions
cannot, and when combined, you can get some cool interactive effects
(although I don't recommend relying on this in scripts):
http://www.chiark.greenend.org.uk/~sgtatham/aliases.html


Interesting article. One thing not mentioned there is declarations.

alias assoc='declare -A '
assoc x y z

Not particularly useful, but you can't replace that with a function (as 
long as there is no 'global' option for declare).


Bernd

--
Bernd Eggink
http://sudrala.de



Re: Issues when func name is the same with an alias

2010-08-04 Thread Clark J. Wang
On Wed, Aug 4, 2010 at 7:03 PM, Marc Herbert marc.herb...@gmail.com wrote:

 Le 04/08/2010 11:39, Clark J. Wang a écrit :

  Seems like I must explicitly use the `function' keyword to define foo()
 for
  this scenario. Is that the correct behavior?

 The correct behaviour is simply not to use aliases, since they bring
 nothing
 to the table compared to functions. Have a look at this:
 http://thread.gmane.org/gmane.comp.shells.bash.bugs/13865/focus=13901

 I do not agree. Aliases are much simpler to use than functions. I use
almost all (51 out of 52) the single letters ([:alpha:]) to define aliases.
I would not like a shell which has no aliases support.


 About the function keyword have a look at the discussion two days ago.



Re: Issues when func name is the same with an alias

2010-08-04 Thread Bernd Eggink

Am 04.08.2010 16:38, schrieb Clark J. Wang:

On Wed, Aug 4, 2010 at 8:27 PM, Bernd Egginkmono...@sudrala.de  wrote:


Am 04.08.2010 12:39, schrieb Clark J. Wang:

  I was testing the precedence between functions and aliases so I tried like

this (with bash 4.1.5):

$ cat rc
alias foo='echo this is the alias'

foo()
{
 builtin echo 'this is the function'
}

foo
$ source rc
bash: confusing-aliases-2.sh: line 4: syntax error near unexpected token
`('
bash: confusing-aliases-2.sh: line 4: `foo()'
$

Seems like I must explicitly use the `function' keyword to define foo()
for
this scenario. Is that the correct behavior?



The man page says The first word of a simple command, if unquoted, is
checked to see if has an alias. Therefore 'foo' in your function
declaration is replaced by 'echo this is the alias'. Unfortunately, you
can't quote the function name in the declaration, so you have to either use
'function' or say unalias foo first.


Function definitions are not simple commands. Actually, func definition
syntax is listed under the *Compound Commands* section in bash2.05b's man
page and in bash3+ it's been moved to a separate section.


The function *body* as part of the definition is a compound command. The 
definition as a whole constitutes a simple comand. You can see that 
function definitions are not listed under compound commands, but 
adhere to the definition of simple commands:


A  simple  command is a sequence of optional variable assignments 
followed by blank-separated words and redirections, and terminated by a 
control operator.


Some lines above '(' is listed as a control operator.

The only questionable term is ...a sequence of optional variable 
assignments followed by..., as it appears that variable assignments are 
not allowed before a function definition.


I suppose that is just a documentation issue. Can anybody comment on this?

Regards,
Bernd

--
Bernd Eggink
http://sudrala.de



Re: Issues when func name is the same with an alias

2010-08-04 Thread Chet Ramey
On 8/4/10 10:38 AM, Clark J. Wang wrote:

 Function definitions are not simple commands. Actually, func definition
 syntax is listed under the *Compound Commands* section in bash2.05b's man
 page and in bash3+ it's been moved to a separate section.

While technically true, that doesn't enter into the issue.  At the time
a word is read, the parser can't tell whether or not it introduces a
simple command or function definition.  Posix, for instance, explicitly
notes that alias expansion is performed before examining any grammar rules,
and certainly without performing any lookahead.  I probably cribbed the
simple command language from Posix, which doesn't allow reserved words,
which introduce compound commands, to be aliased.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/