zsh-users
 help / color / mirror / code / Atom feed
From: Ray Andrews <rayandrews@eastlink.ca>
To: zsh-users@zsh.org
Subject: Re: this should be easy variable expansion including globs.
Date: Wed, 18 Jan 2017 11:51:35 -0800	[thread overview]
Message-ID: <5ade5a22-d115-ed6f-62e2-9d69a9db20d3@eastlink.ca> (raw)
In-Reply-To: <20170118184623.3bf91c52@ntlworld.com>

On 18/01/17 10:46 AM, Peter Stephenson wrote:
> On Wed, 18 Jan 2017 08:13:54 -0800
> Ray Andrews <rayandrews@eastlink.ca> wrote:
>> $ var1=var1
>> $ var2=var2
>> $ var3=var3
>> $ var99=var99
>> $ for f ($var*) echo $f
> I'm going to regret this, but...
What?  Who me, make a stink about it?  Never.

Seriously, I'd do that Peter if I was creating the variables, and my 
example makes it look like that, but in fact I'm trying to discover some 
variables created by a program. It's the 'smartd/smart-notifier' utility 
which creates a slew of variables all starting with 'SMARTD...' and 
since it's run via script they all evaporate before I can see what's 
available to me so I'm wanting to print out 'SMARTD*', that is, all 
variables starting that way whatever they may be.  I had thought of the 
clumsy:

$ set | grep SMARTD >! somefile
$ cat somefile

... but I expect it can be done elegantly.
===============================
>
> The right way of doing this is to use an array.  It's very similar to
> what you've got except you refer to $var[1] rather than $var1.  That
> index means the shell knows roughly what you've got on your mind from
> the start.
>
> The following syntax may look too good to be true, but does work...
>
> var=(var{1..99})
>
> This is equivalent to
>
> typeset -a var
> var[1]=var1
> var[2]=var2
> ...
>
> The first line is there to ensure var is an array.  This is a useful
> example as it shows that the array grows as you need it to.
>
> Now the simple "echo" you've got above can be done as
>
> print -lr -- $var
>
> The -l prints one entry per line.  The -r stops any clever expansions so
> you get exactly what's in the array.
>
> For most operations, you probably need more control over what you're
> doing with entries.  Depending how complicated it gets, your main choices
> are the following.
>
> Process every non-empty array entry, regardless of number:
>
> for elt in $var; do
>    # $elt in turn refers to elements of the array
>    print -r -- $elt
> done
>
> Process every entry whether it's empty or not --- there's no distinction
> between the two with the values above, it's just a bit of arcanery in
> case you need it.
>
> for elt in "${var[@]}"; do
>    print -r -- $elt
> done
>
> Loop over all 99 elements of... well, anything, but in this case that
> array:
>
> integer i
> for (( i = 1; i <= 99; i++ )); do
>    print -r -- $var[i]
> done
>
> If you nonetheless still want to do some completely different, someone
> else will no doubt be along in a minute for the usual loooooooong
> argument.
>
> pws
>


  reply	other threads:[~2017-01-18 19:51 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-18 16:13 Ray Andrews
2017-01-18 18:46 ` Peter Stephenson
2017-01-18 19:51   ` Ray Andrews [this message]
2017-01-18 20:12     ` Nikolay Aleksandrovich Pavlov (ZyX)
2017-01-18 20:30       ` Ray Andrews
     [not found]     ` <484161484770354__27207.6165573255$1484770824$gmane$org@web26m.yandex.ru>
2017-01-18 20:20       ` Daniel Shahaf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5ade5a22-d115-ed6f-62e2-9d69a9db20d3@eastlink.ca \
    --to=rayandrews@eastlink.ca \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).