Re: RFE -or- Howto?

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

On Mon, 2 Aug 2010, Linda Walsh wrote:


But then we've devolved to really ugly.

I like the look of
(a b c) = (1 2 3).

At least the "<<<" allows it to be on one line though not so attractively,
but using the multiline redirection just gets too unslightly for words.


A minor loss of "prettiness" is negligible compared to
portability.


It makes it clear that you are no longer doing what I want, a multi-variable
assignment.


It *is* a multi-variable assignment.


But that's my sense of what looks readable in codeYMMV.


On 8/2/2010 3:51 PM, Chris F.A. Johnson wrote:

On Mon, 2 Aug 2010, Dennis Williamson wrote:


It's called a "here string".


And the same thing can also be accomplished with the more portable
here document:

read a b c d <<.
1 2 3 4
.




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



Re: RFE? request for an "undefined" attribute for functions

2010-08-02 Thread Jan Schampera

Linda Walsh wrote:


On 8/2/2010 1:13 PM, Chet Ramey wrote:

There are several versions of `autoload' in examples/functions.

Chet

===
I've been using 'man bash' as my reference.  I don't see a reference
to examples or autoload, and finding 'functions' doesn't show me any examples.
Is there another manpage I should be regularly consulting?


It's a directory in the Bash distribution.

Jan




Re: RFE? request for an "undefined" attribute for functions

2010-08-02 Thread Greg Wooledge
On Mon, Aug 02, 2010 at 01:20:58PM -0700, Linda Walsh wrote:
> On 8/2/2010 1:13 PM, Chet Ramey wrote:
> > There are several versions of `autoload' in examples/functions.
> > 
>   I've been using 'man bash' as my reference.  I don't see a reference
> to examples or autoload, and finding 'functions' doesn't show me any examples.
> Is there another manpage I should be regularly consulting?

"examples" is a subdirectory of the bash source code.  "functions" is
a subdirectory underneath that.



Re: RFE? request for an "undefined" attribute for functions

2010-08-02 Thread Linda Walsh


On 8/2/2010 1:13 PM, Chet Ramey wrote:
> There are several versions of `autoload' in examples/functions.
> 
> Chet
===
I've been using 'man bash' as my reference.  I don't see a reference
to examples or autoload, and finding 'functions' doesn't show me any examples.
Is there another manpage I should be regularly consulting?

Thanks!
-linda



Re: RFE? request for an "undefined" attribute for functions

2010-08-02 Thread Chet Ramey
On 8/1/10 9:10 PM, Linda Walsh wrote:
> 
> 
> 
> I had(have) several functions that I don't use on a regular basis (rarely), 
> that
> I had put into a subdir "func_lib" under my local-definitions directory.
> This came from ksh, which allows you to define functions with an "undef" 
> attribute,
> and at runtime, the first time these functions were referenced,
> 
> Is this something that might have been considered for bash?  It seems like it
> could have some usefulness?

There are several versions of `autoload' in examples/functions.

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/



Re: weird behaviour of ((count++)) when using , , to change to lower case

2010-08-02 Thread Chet Ramey
On 8/1/10 7:05 PM, Dennis Williamson wrote:
> If I do the echo line twice, I get a segfault in both Bash
> 4.0.33(1)-release and 4.1.0(1)-release.

Thanks.  This will be fixed in bash-4.2.

> And you're right about being evaluated twice.

This was already changed after bash-4.1 was released; the change will be
in bash-4.2.

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/



Re: Indirect expansion and arrays

2010-08-02 Thread Chet Ramey
On 7/29/10 4:55 PM, Bernd Eggink wrote:
> It seems that indirect expansion doesn't work with arrays:
> 
> $ a=(x y z)
> $ b=a
> $ echo "${!b[0]} ${!b[1]} ${!b[2]}"
> x
> 
> Is that intended? The documentation isn't explicit about it.

It does, but it doesn't work in the way you are trying.  The `!' binds to
an entire variable reference, in this case 'b[0]'.  The idea behind that
was to permit the use of an array of variable names, for instance, that
could be easily referenced using indirect expansion.

The following code will display
"x variable y variable z variable"

a=(x y z)

x='x variable'
y='y variable'
z='z variable'

echo "${!a[0]} ${!a[1]} ${!a[2]}"

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/



Re: RFE? request for an "undefined" attribute for functions

2010-08-02 Thread Greg Wooledge
On Mon, Aug 02, 2010 at 09:11:23PM +0200, Bernd Eggink wrote:
> Ksh and zsh also have the 'function' 
> keyword, probably other shells as well. I prefer it in ksh because it 
> makes locally declared variables really local, while with the name() 
> syntax they are shared with the environment.

What?!

imadev:~$ ksh -c 'unset var; var=main; foo() { typeset var; var=local; }; foo; 
echo $var'
main

That's ksh88.

arc3:~$ ksh -c 'unset var; var=main; foo() { typeset var; var=local; }; foo; 
echo $var'
local

That's ksh93.

Oh, how fun.  Whee!  Good thing I'm using bash, not ksh.



Re: RFE? request for an "undefined" attribute for functions

2010-08-02 Thread Bernd Eggink

Am 02.08.2010 20:16, schrieb Eric Blake:

On 08/02/2010 12:15 PM, Bernd Eggink wrote:

Am 02.08.2010 19:15, schrieb Andreas Schwab:

Bernd Eggink   writes:


  eval "function $name


Don't use function, use "$name ()" instead.


What's wrong with function??


'function name' is a bash extension while 'name()' is POSIX.  If you use
standard POSIX instead of bash extensions, then your approach will more
easily port to other POSIX shells.


It's not just a bash extension. Ksh and zsh also have the 'function' 
keyword, probably other shells as well. I prefer it in ksh because it 
makes locally declared variables really local, while with the name() 
syntax they are shared with the environment. That's one reason why it 
became a habit. The other is that 'function' is clear and 
self-explaining, while 'name()' wrongly suggests that function 
parameters should be surrounded by parentheses.
Apart from that, I can't see why I should care for POSIX when writing 
bash-specific hacks.


Regards,
Bernd

--
Bernd Eggink
http://sudrala.de



Re: RFE? request for an "undefined" attribute for functions

2010-08-02 Thread Eric Blake
On 08/02/2010 12:15 PM, Bernd Eggink wrote:
> Am 02.08.2010 19:15, schrieb Andreas Schwab:
>> Bernd Eggink  writes:
>>
>>>  eval "function $name
>>
>> Don't use function, use "$name ()" instead.
> 
> What's wrong with function??

'function name' is a bash extension while 'name()' is POSIX.  If you use
standard POSIX instead of bash extensions, then your approach will more
easily port to other POSIX shells.

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



signature.asc
Description: OpenPGP digital signature


Re: RFE? request for an "undefined" attribute for functions

2010-08-02 Thread Bernd Eggink

Am 02.08.2010 19:15, schrieb Andreas Schwab:

Bernd Eggink  writes:


 eval "function $name


Don't use function, use "$name ()" instead.


What's wrong with function??

Bernd

--
Bernd Eggink
http://sudrala.de



Re: RFE? request for an "undefined" attribute for functions

2010-08-02 Thread Andreas Schwab
Bernd Eggink  writes:

> eval "function $name

Don't use function, use "$name ()" instead.

Andreas.

-- 
Andreas Schwab, sch...@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."



Re: RFE? request for an "undefined" attribute for functions

2010-08-02 Thread Bernd Eggink

Am 02.08.2010 03:10, schrieb Linda Walsh:




I had(have) several functions that I don't use on a regular basis (rarely), that
I had put into a subdir "func_lib" under my local-definitions directory.
This came from ksh, which allows you to define functions with an "undef" 
attribute,
and at runtime, the first time these functions were referenced,

Is this something that might have been considered for bash?  It seems like it
could have some usefulness?


If you are concerned about memory usage, you could use a mechanism like 
this:


#-
function undef
{
local name

for name
do
eval "function $name
{
source $FUNCDIR/$name
$name \"\...@\"
}"
done
}
#-

The call

undef f1 f2 f3

(corresponding to 'autoload f1 f2 f3' in ksh) creates small placeholder 
functions f1, f2, and f3. The first call to any of these functions will 
replace its definition by the one found in $FUNCDIR, and also call the 
latter.
I'm not sure, however, if this is guaranteed to work in any case (and in 
any bash version).


Greetings,
Bernd

--
Bernd Eggink
http://sudrala.de



Re: RFE? request for an "undefined" attribute for functions

2010-08-02 Thread Greg Wooledge
On Sun, Aug 01, 2010 at 06:10:31PM -0700, Linda Walsh wrote:
> I had(have) several functions that I don't use on a regular basis (rarely), 
> that
> I had put into a subdir "func_lib" under my local-definitions directory.
> This came from ksh, which allows you to define functions with an "undef" 
> attribute,
> and at runtime, the first time these functions were referenced,

(You lost some words here.)

> Is this something that might have been considered for bash?  It seems like it
> could have some usefulness?

There is a command_not_found_handle in bash 4.  You could define it to
look for functions in your directory.



Re: RFE -or- Howto?

2010-08-02 Thread Dennis Williamson
It's called a "here string".

On Mon, Jul 26, 2010 at 8:02 PM, Linda Walsh  wrote:
> Huh. Triple redirect...  Thanks!
>
>
> On 7/26/2010 5:53 PM, Chet Ramey wrote:
>> On 7/26/10 6:25 PM, Linda Walsh wrote:
>>>  I don't know if there's an easy way, but if not would you consider an
>>> RFE --
>>>
>>> Is there a syntax for a mult-var assignment, ala:
>>> (a b c d)=(1 2 3 4)
>>
>> Yes.
>>
>> read a b c d <<<"1 2 3 4"
>>
>> Chet
>
>