bug-bash
[Top][All Lists]
Advanced

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

manpage note? weird strings that appear to be equal but create haywire c


From: Linda Walsh
Subject: manpage note? weird strings that appear to be equal but create haywire comparisons?
Date: Wed, 26 Aug 2009 02:45:39 -0700
User-agent: Thunderbird 2.0.0.22 (Windows/20090605)

I was scripting and fixing some permissions in Win,
and had a var 'c' set to (no quotes in the var):   'C\windows\system32'

 # echo  $v |hexdump -C
 00000000  43 3a 5c 77 69 6e 64 6f  77 73 5c 73 79 73 74 65  |C:\windows\syste|
 00000010  6d 33 32 0a                                       |m32.|
 00000014

I first bumped into this using the printf -v var "%q" feature, where
I expected it to doublequote the back slashes. But instead, it looked
like I got the same value assigned to v as though I'd just done a 'c=v'

# printf -v v "%q" $c
# echo  $v |hexdump -C
00000000  43 3a 5c 77 69 6e 64 6f  77 73 5c 73 79 73 74 65  |C:\windows\syste|
00000010 6d 33 32 0a 00000014

But then I tested equality on the strings and that's the confusing
part. I have an idea of what's going on but boy do string compares look
confused.  They perform same on cygwin
(bashv=3.2.49(22)-release (i686-pc-cygwin)) and Suse11.1:
(bashv=3.2.39(1)-release (x86_64-suse-linux-gnu)

Using the simple form:
   if <expr> ; then echo = "True" else echo False ; fi
I get:
for c=C:\Windows\System32, and v=C:\Windows\System32
expr = [[ "$c" = "$v" ]]  : "False"
expr = [ "$c" = "$v" ]    : "False"
expr = [[ "$c" != "$v" ]] : "True"
expr = [ "$c" != "$v" ]   : "True"
expr = [[ $c = $v ]]      : "True"
expr = [ $c = $v ]        : "False"
expr = [[ $c != $v ]]     : "False"
expr = [ $c != $v ]       : "True"

Note that [[ and [ return different results when the vars are unquoted.

TestProg: (interactive shell...)

{
c='C:\Windows\System32'
printf -v v "%q" "$c"
export c v echo for c=$c, and v=$v
for ((qq=2;qq>=0;qq-=2)); do
 if ((qq==2)); then q='"'; else q=''; fi
 for test in '=' '!=' ; do
  for ((op=1;op<=2;++op)) ; do
   if (($op==1)) ; then to="[["; tc="]]" ; else to="["; tc="]"; fi
          expr=$(printf "%s %s%s%s %s %s%s%s %s" "$to" "$q" '$c' "$q" "$test" "$q" '$v' 
"$q" "$tc")
         printf "expr = %-18s : \"" "$expr"
        if eval $expr ; then echo True\"  ; else echo False\" ; fi
  done
 done
done;
# vim:et:ts=1:sw=1
}


------
I sorta understand why they wouldn't be entirely equal, but having the
built-in treat them differently than the single brackets is a bit
surprising..



If I put them outside




reply via email to

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