[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: manpage note? weird strings that appear to be equal but create haywi
From: |
Greg Wooledge |
Subject: |
Re: manpage note? weird strings that appear to be equal but create haywire comparisons? |
Date: |
Wed, 26 Aug 2009 09:19:46 -0400 |
User-agent: |
Mutt/1.4.2.3i |
On Wed, Aug 26, 2009 at 02:45:39AM -0700, Linda Walsh wrote:
> I was scripting and fixing some permissions in Win,
> and had a var 'c' set to (no quotes in the var): 'C\windows\system32'
How did you assign this value? Did you read it from a file? Did you
type a specific bash command?
> # echo $v |hexdump -C
> 00000000 43 3a 5c 77 69 6e 64 6f 77 73 5c 73 79 73 74 65
This isn't trustworthy, because you did not quote $v. The variable
might have leading or trailing whitespace, which the unquoted parameter
expansion would remove before echo gets it.
There's also glob expansion to worry about. Your variable might contain
C:\windows\sy?tem3* for all we know.
> # printf -v v "%q" $c
Same problem here -- unquoted $c means you're not necessarily getting
the variable's exact contents.
> # echo $v |hexdump -C
And here.
> TestProg: (interactive shell...)
>
> {
> c='C:\Windows\System32'
> printf -v v "%q" "$c"
Finally, quotes!
Here's what your v variable should contain after that:
$ c='C:\Windows\System32'; printf -v v %q "$c"; echo "$v"
C:\\Windows\\System32
But your test script says:
> export c v
> echo for c=$c, and v=$v
And your alleged output says:
> for c=C:\Windows\System32, and v=C:\Windows\System32
Even with the unquoted $v here, you should have seen the double backslashes:
$ echo $v
C:\\Windows\\System32
So your output doesn't match your script. Something strange is afoot.
> Note that [[ and [ return different results when the vars are unquoted.
Since we don't really know WHAT your variables contain, any analysis
of that output is a waste of time, although Pierre gave some decent
pointers about the general difference between [ and [[.
On Wed, Aug 26, 2009 at 01:17:07PM +0300, Pierre Gaston wrote:
> Now I'm not too sure why var='"*'"; [[ \* = $var ]] is false while
> var='\*' [[ \* = $var ]] is true
Assuming the first part was supposed to be var='"*"' ...
The bash command [[ \* = $var ]] returns true if $var contains a glob
pattern against which a literal asterisk * can be matched. (By the way,
you don't need the \ there. No glob expansion is done inside [[...]] so
you could use a plain * on the left hand side.)
If $var contains \* then the match is successful. \* as a glob describes
a literal asterisk, which is what we're trying to match.
$ var='\*'; [[ * = $var ]]; echo $?
0
If $var contains "*" (double quote, asterisk, double quote), then
the double quotes are considered part of the glob pattern. A bare
asterisk won't match that glob, because it doesn't have double quotes
attached to it.
imadev:~$ var='"*"'; [[ * = $var ]]; echo $?
1
It only matches double quote, asterisk, double quote:
$ var='"*"'; [[ \"*\" = $var ]]; echo $?
0