Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > gnu.bash.bug > #11744
| Path | csiph.com!goblin3!goblin1!goblin.stu.neva.ru!usenet.stanford.edu!not-for-mail |
|---|---|
| From | Oleg Popov <dev-random@dev-random.ru> |
| Newsgroups | gnu.bash.bug |
| Subject | Re: Dynamic variable failure & equiv-const strings compare unequal |
| Date | Thu, 22 Oct 2015 13:58:16 +0300 |
| Lines | 128 |
| Approved | bug-bash@gnu.org |
| Message-ID | <mailman.786.1445518708.7904.bug-bash@gnu.org> (permalink) |
| References | <5628B3E2.70405@tlinx.org> |
| NNTP-Posting-Host | lists.gnu.org |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| X-Trace | usenet.stanford.edu 1445518709 24464 208.118.235.17 (22 Oct 2015 12:58:29 GMT) |
| X-Complaints-To | action@cs.stanford.edu |
| Cc | bug-bash@gnu.org |
| To | Linda Walsh <bash@tlinx.org> |
| Envelope-to | bug-bash@gnu.org |
| Content-Disposition | inline |
| In-Reply-To | <5628B3E2.70405@tlinx.org> |
| X-detected-operating-system | by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] |
| X-Received-From | 5.255.227.21 |
| X-Mailman-Approved-At | Thu, 22 Oct 2015 08:58:28 -0400 |
| X-BeenThere | bug-bash@gnu.org |
| X-Mailman-Version | 2.1.14 |
| Precedence | list |
| List-Id | Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org> |
| List-Unsubscribe | <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe> |
| List-Archive | <http://lists.gnu.org/archive/html/bug-bash> |
| List-Post | <mailto:bug-bash@gnu.org> |
| List-Help | <mailto:bug-bash-request@gnu.org?subject=help> |
| List-Subscribe | <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe> |
| Xref | csiph.com gnu.bash.bug:11744 |
Show key headers only | View raw
On Thu, Oct 22, 2015 at 03:01:06AM -0700, Linda Walsh wrote:
> [cut]
> I.e. test output was:
> Case 2 got/Expected:
> "222"
> "1\ 222\ .3\ .4"
> [cut]
You didn't initialize the array. By the time you do "parts[1]=222" it's
still empty. And in your previous message you tried to initialize it in
a subshell. Variables don't retain their values after returning from
subshells.
> [cut]
> test 3 --- was supposed to check the values
> of the hash field-by-field, but first I thought to check
> them when printed out in a specific order as 2 identical
> strings. The strings look identical, yet don't compare equal.
> so it seemed pointless to try to break down test 3's output
> into a field-by-field comparison when I couldn't even get
> the identical strings that contained the fields to compare.
> [cut]
> [cut]
> if [[ $out != $exp ]]; then
> [cut]
Bash uses unquoted characters on the right side of == and != as wildcard
patterns. For example, [ipaddr] in $exp means "any of i, p, a, d, r".
You should quote the right operand.
> #!/bin/bash
>
> shopt -s expand_aliases ; alias my=declare
> alias array='my -a' int='my -i'
>
> ip=10.20.30.40
> array parts=()
> array answers=( '10 20 30 40' '1 .2 .3 .4' '1 222 .3 .4'
> '192.168.0.1/24::{ [ipaddr]="192.168.0.1/24" [address]="192.168.0.1"
> [prefixlen]="24" ; }'
> )
>
>
> array addr_fields=(ipaddr address prefixlen)
>
> assignparts() { parts=( $ip ) ; }
>
> tst0 () { my IFS=. ; assignparts ; echo -E "${parts[@]}"; }
> tst1 () { my IFS=0 ; assignparts ; echo -E "${parts[@]}"; }
> tst2 () { parts[1]=222; echo -E "${parts[@]}" ; }
>
> tst3 () {
> my ipaddr="$1"; shift;
> int status=0
> my pat='^([^/]+)/([0-9]+)\s*$'
> my address prefixlen
> if [[ ! $ipaddr =~ $pat ]]; then
> echo >&2 "Error in ip/netsize format: \"$ipaddr\""
> status=1
> else
> address=${BASH_REMATCH[1]}
> prefixlen=${BASH_REMATCH[2]}
> my out=""
> for flds in "${addr_fields[@]}"; do
> out+="[$flds]=\"${!flds}\" "
> done
> printf "{ %s; }" "$out"
> fi
> return $status
> }
>
>
>
> int passno=0 tests=0
> my fmt="Case %d got/Expected:\n \"%q\"\n \"%q\"\n"
>
>
> testor () {
> int tstno
> my out=""
> for tstno in {0..3}; do
> tests+=1
> exp="${answers[tstno]}"
> if [[ $exp =~ :: ]]; then
> my args="${exp%::*}"
> exp="${exp#*::}"
> out="$(tst$tstno $args)"
> else
> out="$(tst$tstno)"
> fi
> if [[ $out != $exp ]]; then
> printf >&2 "$fmt" "$tstno" "$out" "$exp"
> continue
> fi
> passno+=1
> done
> }
>
> testor
> echo "Passed $passno/$tests tests."
> ----------------
>
> output:
> Case 2 got/Expected:
> "222"
> "1\ 222\ .3\ .4"
> Case 3 got/Expected:
> "\{\ \[ipaddr\]=\"192.168.0.1/24\"\ \[address\]=\"192.168.0.1\"\
> \[prefixlen\]=\"24\"\ \;\ \}"
> "\{\ \[ipaddr\]=\"192.168.0.1/24\"\ \[address\]=\"192.168.0.1\"\
> \[prefixlen\]=\"24\"\ \;\ \}"
> Passed 2/4 tests.
>
> The outputs for case 3 look identical -- I was using %s to print
> them out, but switched to "%q", to ensure no hidden chars...
>
> case 2 --
> ??? Why didn't it only change the 1 member?
>
>
>
>
>
>
>
Back to gnu.bash.bug | Previous | Next | Find similar
Re: Dynamic variable failure & equiv-const strings compare unequal Oleg Popov <dev-random@dev-random.ru> - 2015-10-22 13:58 +0300
csiph-web