Gentoo Archives: gentoo-dev

From: David Leverton <u01drl3@×××××××.uk>
To: gentoo-dev@l.g.o
Subject: [gentoo-dev] Re: Network configuration and bash
Date: Fri, 09 Feb 2007 14:15:22
Message-Id: eqhvf7$m9s$1@sea.gmane.org
In Reply to: Re: [gentoo-dev] Network configuration and bash by Roy Marples
1 Roy Marples wrote:
2 > On Fri, 09 Feb 2007 01:03:04 -0800
3 > Donnie Berkholz <dberkholz@g.o> wrote:
4 >
5 >> How about this?
6 >>
7 >> replace="
8 >> '4d 1280 768 24'
9 >> '5c 1400 1050 16'
10 >> "
11 >>
12 >
13 > Actually, that may work better than my delimited with ; approach.
14 > We could then do
15 >
16 > eval set -- "${replace}"
17 > for x in "$@" ; do
18 > ....
19 > done
20
21 Would something like the following be acceptable? If the user uses bash
22 they can use an array, otherwise (or if they prefer) they can do the
23 "'...' '...'" thing, transparently to the code that uses the variable.
24 Could do with more testing of edge cases, but it seems to work well for
25 me - in bash as is, and in dash and busybox with the "foo" section
26 commented out.
27
28 get_array() {
29 local var="${1}"
30
31 if [ -n "${BASH}" ]; then
32 declaration="$( declare -p "${var}" )"
33 case "${declaration}" in
34 "declare -a"*)
35 echo "set -- \"\${${var}[@]}\""
36 return
37 esac
38 fi
39
40 echo "eval set -- \"\${${var}}\""
41 }
42
43 foo=( 1 2 3 )
44 eval $(get_array foo)
45 for x in "$@"; do
46 echo $x
47 done
48
49 bar="1 2 3"
50 eval $(get_array bar)
51 for x in "$@"; do
52 echo $x
53 done
54
55
56 --
57 gentoo-dev@g.o mailing list

Replies

Subject Author
Re: [gentoo-dev] Re: Network configuration and bash Roy Marples <uberlord@g.o>