From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29465 invoked by alias); 18 Jan 2017 19:51:41 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 22399 Received: (qmail 20899 invoked from network); 18 Jan 2017 19:51:41 -0000 X-Qmail-Scanner-Diagnostics: from mta01.eastlink.ca by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1. Clear:RC:0(24.224.136.30):SA:0(-3.9/5.0):. Processed in 1.062328 secs); 18 Jan 2017 19:51:41 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-3.9 required=5.0 tests=RCVD_IN_DNSWL_LOW, RP_MATCHES_RCVD,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.1 X-Envelope-From: rayandrews@eastlink.ca X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | Received-SPF: pass (ns1.primenet.com.au: SPF record at _spf.eastlink.ca designates 24.224.136.30 as permitted sender) X-Authority-Analysis: v=2.2 cv=ZvqvEJzG c=1 sm=1 tr=0 a=28Ntk8jg+Dho8ABWn/CRtA==:117 a=28Ntk8jg+Dho8ABWn/CRtA==:17 a=N659UExz7-8A:10 a=RGJ_-vN7deGExORnxJMA:9 a=pILNOxqGKmIA:10 X-EL-IP-NOAUTH: 24.207.16.108 Subject: Re: this should be easy variable expansion including globs. To: zsh-users@zsh.org References: <52be7a1a-2da8-a4b6-905a-3eea694763c3@eastlink.ca> <20170118184623.3bf91c52@ntlworld.com> From: Ray Andrews Message-id: <5ade5a22-d115-ed6f-62e2-9d69a9db20d3@eastlink.ca> Date: Wed, 18 Jan 2017 11:51:35 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Icedove/45.4.0 MIME-version: 1.0 In-reply-to: <20170118184623.3bf91c52@ntlworld.com> Content-type: text/plain; charset=windows-1252; format=flowed Content-transfer-encoding: 7bit On 18/01/17 10:46 AM, Peter Stephenson wrote: > On Wed, 18 Jan 2017 08:13:54 -0800 > Ray Andrews 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 >