Gentoo Archives: gentoo-dev

From: Roy Marples <uberlord@g.o>
To: gentoo-dev@l.g.o
Subject: Re: [gentoo-dev] Re: Network configuration and bash
Date: Fri, 09 Feb 2007 15:47:10
Message-Id: 20070209154433.354956de@uberlaptop
In Reply to: [gentoo-dev] Re: Network configuration and bash by David Leverton
1 On Fri, 09 Feb 2007 14:16:25 +0000
2 David Leverton <u01drl3@×××××××.uk> wrote:
3 > Would something like the following be acceptable? If the user uses
4 > bash they can use an array, otherwise (or if they prefer) they can do
5 > the "'...' '...'" thing, transparently to the code that uses the
6 > variable. Could do with more testing of edge cases, but it seems to
7 > work well for me - in bash as is, and in dash and busybox with the
8 > "foo" section commented out.
9 >
10 > get_array() {
11 > local var="${1}"
12 >
13 > if [ -n "${BASH}" ]; then
14 > declaration="$( declare -p "${var}" )"
15 > case "${declaration}" in
16 > "declare -a"*)
17 > echo "set -- \"\${${var}[@]}\""
18 > return
19 > esac
20 > fi
21 >
22 > echo "eval set -- \"\${${var}}\""
23 > }
24
25 Very nice. We need to redirect 2>/dev/null for declare -p incase the
26 variable is not defined, but otherwise it works well in my tests too :)
27
28 Thanks
29
30 Roy
31 --
32 gentoo-dev@g.o mailing list