zsh-users
 help / color / mirror / code / Atom feed
* outputs of _users and _hosts to arrays
@ 2005-07-24  7:05 Wataru Kagawa
  2005-07-24  9:33 ` Bart Schaefer
  0 siblings, 1 reply; 7+ messages in thread
From: Wataru Kagawa @ 2005-07-24  7:05 UTC (permalink / raw)
  To: zsh-users

Hi all,

How can I add the outputs of _users and _hosts to arrays?  I tried,

users=( "$_users[@]" )
hosts=( "$_hosts[@]" )

without luck.

Help is greatly appreciated.

Wataru Kagawa


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: outputs of _users and _hosts to arrays
  2005-07-24  7:05 outputs of _users and _hosts to arrays Wataru Kagawa
@ 2005-07-24  9:33 ` Bart Schaefer
  2005-07-25  5:18   ` Wataru Kagawa
  0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2005-07-24  9:33 UTC (permalink / raw)
  To: zsh-users

On Jul 24,  4:05pm, Wataru Kagawa wrote:
} 
} How can I add the outputs of _users and _hosts to arrays?

Those are completion functions.  They don't have output.  They just
call the compadd builtin.

So look up "compadd" in the manual, and you find:

    -O ARRAY
          If this option is given, the WORDS are _not_ added to the set
          of possible completions.  Instead, matching is done as usual
          and all of the WORDS given as arguments that match the string
          on the command line will be stored in the array parameter
          whose name is given as ARRAY.

And therefore:

    _users -O users
    _hosts -O hosts

However, that only works inside functions called by the completion code.
You cannot employ _users and _hosts for generalized programming.

} users=( "$_users[@]" )
} hosts=( "$_hosts[@]" )

That syntax would mean that _users and _hosts were array variables; but
you seem to know that they're functions, so why did you try that?


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: outputs of _users and _hosts to arrays
  2005-07-24  9:33 ` Bart Schaefer
@ 2005-07-25  5:18   ` Wataru Kagawa
  2005-07-25  5:38     ` Wataru Kagawa
  2005-07-25  9:15     ` Motoi Washida
  0 siblings, 2 replies; 7+ messages in thread
From: Wataru Kagawa @ 2005-07-25  5:18 UTC (permalink / raw)
  To: zsh-users; +Cc: Bart Schaefer

Hi Bart,

Thanks for the help.
After googling, I found out that _users extract usernames from /etc/ 
passwd and _hosts extract hostnames from /etc/hosts.

So, I can use parameter expansions like,

users=( ${$( cat /etc/users | cut -d : -f 1,7 | grep -v "#" )//:*/} )
hosts=( ${$( cat /etc/hosts | cut -d : -f 1,7 | grep -v "#" )//:*/} )

to get what I need.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: outputs of _users and _hosts to arrays
  2005-07-25  5:18   ` Wataru Kagawa
@ 2005-07-25  5:38     ` Wataru Kagawa
  2005-07-25  9:15     ` Motoi Washida
  1 sibling, 0 replies; 7+ messages in thread
From: Wataru Kagawa @ 2005-07-25  5:38 UTC (permalink / raw)
  To: zsh-users

Oops.

> users=( ${$( cat /etc/users | cut -d : -f 1,7 | grep -v "#" )//:*/} )

should be,

users=( ${$( cat /etc/passwd | cut -d : -f 1,7 | grep -v "#" )//:*/} )


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: outputs of _users and _hosts to arrays
  2005-07-25  5:18   ` Wataru Kagawa
  2005-07-25  5:38     ` Wataru Kagawa
@ 2005-07-25  9:15     ` Motoi Washida
  2005-07-26  1:56       ` Wataru Kagawa
  1 sibling, 1 reply; 7+ messages in thread
From: Motoi Washida @ 2005-07-25  9:15 UTC (permalink / raw)
  To: Wataru Kagawa; +Cc: zsh-users

Hi Wataru,

On 07/25/2005, at 14:18, Wataru Kagawa wrote:

> Hi Bart,
>
> Thanks for the help.
> After googling, I found out that _users extract usernames from /etc/ 
> passwd and _hosts extract hostnames from /etc/hosts.
>
> So, I can use parameter expansions like,
>
> users=( ${$( cat /etc/users | cut -d : -f 1,7 | grep -v "#" )//:*/} )
If you do that on Mac OS X, maybe you can't get even your name. ;-)
The more general way is using $userdirs special variable.

Load zsh/parameter module,

% zmodload -i zsh/parameter

and,

% users=( $userdir[(I)*] )


I think you can also get user names by calling hash builtin function.  
But I had to rebuild named directory hash table to get expected  
results on some platforms.

You might need to call this,

% builtin hash -fd

and get hash table entries,

% users=( ${${(f)"$(builtin hash -d)"}%\=*} )

> hosts=( ${$( cat /etc/hosts | cut -d : -f 1,7 | grep -v "#" )//:*/} )
>
> to get what I need.
This has the same problem, but I don't know a better way well. Using  
some functions from Perl or some other language?


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: outputs of _users and _hosts to arrays
  2005-07-25  9:15     ` Motoi Washida
@ 2005-07-26  1:56       ` Wataru Kagawa
  2005-07-26 16:23         ` Motoi Washida
  0 siblings, 1 reply; 7+ messages in thread
From: Wataru Kagawa @ 2005-07-26  1:56 UTC (permalink / raw)
  To: zsh-users; +Cc: Motoi Washida

[-- Attachment #1: Type: text/plain, Size: 499 bytes --]

Hi Motoi,

Thanks much for the help.

> Load zsh/parameter module,
>
> % zmodload -i zsh/parameter
>
> and,
>
> % users=( $userdirs[(I)*] )

This works nicely.

>> hosts=( ${$( cat /etc/hosts | cut -d : -f 1,7 | grep -v "#" )//:*/} )
>>
>> to get what I need.
>>
> This has the same problem, but I don't know a better way well.  
> Using some functions from Perl or some other language?

Do you mean that the commands inside the parameter expansion may work  
on Mac OS X but not on other computers?

[-- Attachment #2: Type: text/html, Size: 1276 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: outputs of _users and _hosts to arrays
  2005-07-26  1:56       ` Wataru Kagawa
@ 2005-07-26 16:23         ` Motoi Washida
  0 siblings, 0 replies; 7+ messages in thread
From: Motoi Washida @ 2005-07-26 16:23 UTC (permalink / raw)
  To: Wataru Kagawa; +Cc: zsh-users

On 07/26/2005, at 10:56, Wataru Kagawa wrote:

>>> hosts=( ${$( cat /etc/hosts | cut -d : -f 1,7 | grep -v  
>>> "#" )//:*/} )
>>>
>>> to get what I need.
>>>
>> This has the same problem, but I don't know a better way well.  
>> Using some functions from Perl or some other language?
>
> Do you mean that the commands inside the parameter expansion may  
> work on Mac OS X but not on other computers?
On some systems using NIS, you can get host names of your network  
from shared database, IIRC.


But I think you can get better results by setting hosts manually.

% zstyle ':completion:*' hosts ${${(f)"$(<~/.ssh/known_hosts)"}%% 
[[:space:],]*}

see http://www.zsh.org/mla/users/2005/msg00084.html


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2005-07-26 16:23 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-07-24  7:05 outputs of _users and _hosts to arrays Wataru Kagawa
2005-07-24  9:33 ` Bart Schaefer
2005-07-25  5:18   ` Wataru Kagawa
2005-07-25  5:38     ` Wataru Kagawa
2005-07-25  9:15     ` Motoi Washida
2005-07-26  1:56       ` Wataru Kagawa
2005-07-26 16:23         ` Motoi Washida

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).