bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: conditional aliases are broken


From: Linda Walsh
Subject: Re: conditional aliases are broken
Date: Thu, 18 Aug 2011 08:03:41 -0700
User-agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.24) Gecko/20100228 Thunderbird/2.0.0.24 Mnenhy/0.7.6.666





Pierre Gaston wrote:
Is this a question? or are you trying to make a point?
It is both -- a chance to find answer to a question, or make
the point that aliases in scripts have a useful purposes that
can't be replaced by function clls.
For the question (If I understand correctly):
1) Most variables don't need to be declared in bash.
----
   But in doing so, you end up with non-deterministic behavior
depending on who was in your call stack:

Ex:
 a=1 b=2
 function c { a=3 b=5; }
 c; echo $a, $b
3, 5
# perfectly reasonable

Now supposed 'c' is called by another function 'a':
 function a { local a='tmp' ; c }
 a=1 b=2
 a; echo $a $b
1,5
# IF the same person wrote both, then maybe they
won't be surprised at the behavior, but it certainly
would be regarded as unpredictable, depending on how
intervening functions, -- hardly a great design point,
thought it can allow for some inscrutably designed programs!


The above situation can be worse:
 a=1 b=2
 function c { ((a*=3))||: ; ((b*=5))||: ; }
 c; echo $a, $b
3,  10
# but insert 'a' into the stream
 function a { local a='tmp' ; c }
 a=1 b=2
# now instead of unexpected output we get:
 a
-bash: ((: a: expression recursion level exceeded (error token is "a")

Thus are some examples of dangers in NOT declaring
vars in sub functions.


.2 introduce a new -g to declare a global variable inside a function.
----
   Which doesn't say what it would do in situations like the above.

   Does it declare globals that will have an 'empty value' above the ones
declared in the main prog?  then
trying to use them as in a*=3, b=*=5 would result in failure
as the global vars would be uninitialized.

Anyway, hopefully some problems will be fixed in by 4.2,
especially things like pressing tab on blank lines inside a single
quote expanding to all of your local filenames  --
usually expansions are not performed inside a single quote == let
alone having it desirable to try to expand null
/ empty strings when the option to do so is turned off.


For the point:
Yes the manual says "most" not "all", one interesting hack with aliases is:
http://www.chiark.greenend.org.uk/~sgtatham/aliases.html
----
Indeed.  Some for get the "most" part...

When I asked about defining a local var in a command in the context
of the caller, -- greg didn't even think of aliases, just inferred it
couldn't be done except by doing it in the caller -- i.e. manually
typing it in each time..  Not good!






reply via email to

[Prev in Thread] Current Thread [Next in Thread]