help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] indirection for arrays


From: Steven W. Orr
Subject: Re: [Help-bash] indirection for arrays
Date: Mon, 07 May 2012 09:22:34 -0400
User-agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.28) Gecko/20120306 Thunderbird/3.1.20

On 5/7/2012 9:16 AM, Greg Wooledge wrote:
On Mon, May 07, 2012 at 09:12:52AM -0400, Steven W. Orr wrote:
and I can also say stuff like:

typeset -a flist
glob_array flist "$srpms/$dist/elx-lpfc-*_$dist.src.rpm"

How is that better than (or even different from):

flist=("$srpms/$dist"/elx-lpfc-*_"$dist".src.rpm)

Mine is better because it sets and restores nullglob. My flist will be a zero length list if the glob does not match any files.

Here it is if anyone wants it...

# Take pairs of glob patterns and array names and set the array names to the
# resulting glob patterns. If the glob doesn't match then the array is
# left empty.
glob_array()
{
    typeset pattern
    typeset -i ii
    typeset -a aval
    typeset -i nullglob_status=0
    typeset -r nullglob_reset=su        # Set Unset
    typeset -i nn=0

    (( $# & 1 )) && die 'ERROR: glob_array takes an even number of args'
    shopt -p nullglob > /dev/null
    nullglob_status=$?
    shopt -s nullglob

    while (( $# ))
    do
        typeset $1
        pattern="$2"
        aval=( $pattern )
        address@hidden
        # This should never fail as long as $nn matches passing all of aval.
        # Check anyways for extra credit.
        upvars -a$nn $1 "address@hidden" || die 'ERROR:upvars failed.'
        shift 2
    done
    shopt -${nullglob_reset:$nullglob_status:1} nullglob
}



--
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net



reply via email to

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