On 10/10/10 7:09 PM, Devin Teske wrote:
On Oct 9, 2010, at 10:25 PM, Julian Elischer wrote:

For what it matters, I'v enever found the [ "x$foo" = "x" ] construct to be 
useful.
the quoting seems to work for everything I've ever worked on.

There have been times where I had scripts which could get errors unless "x$foo" was used, but it's been more than 10 years since I last hit that situation. Of course, ever since I did hit it, I tend to write my 'test' parameters in ways which avoid the problem. It might have only been when checking if the variable was set to anything. Eg, using:

if [ "$foo" ] ; then ....

instead of:

if [ -n "$foo" ] ; then ...

Or it might have been when combining multiple checks in a single 'test' (using -a's, etc). However, I'm not going to try to dream up a pathological example of that right now.

I agree... I think that the "x" syntax came around for when people were using 
non-quoted syntax... for example...

[ x$foo = x ]

is still very useful in that it prevents the error when $foo expands to "-e".
The non-quoted example is dangerous in the case where $foo has multiple words in it. The "x" does not save you from that problem. I have a 'list_args' script which just lists out the parameters it is called with:

# Test="this is a multi-word test"
# list_args x$Test

list_args at 19:22:27 Oct 10: $# = 5
ARG[000] l=005: 'xthis'
ARG[001] l=002: 'is'
ARG[002] l=001: 'a'
ARG[003] l=010: 'multi-word'
ARG[004] l=004: 'test'

However, enclosing the argument (as the 'x$foo' portion is really just the 
first argument to the '[' built-in) in quotes:

[ "$foo" = x ]

makes it so that the expansion is taken as:

[ "-n" = x ]

rather than:

[ -n = x ]

The former not causing an error, while the latter does.
The latter does not cause an error. Try it:

# [ "-n" = x ] ; echo $?
1

# [ -e = "no" ] ; echo $?
1

# [ -e = -n ] ; echo $?
1

Quite functionally, at a C-level, if you have your array, ...

argv[0] = "[\0";
argv[1] = "\"-n\"\0"; /* quoted syntax */
argv[2] = "=\0";
argv[3] = "x\0";

and

argv[0] = "[\0";
argv[1] = "-n\0"; /* non-quoted syntax */
argv[2] = "=\0";
argv[3] = "x\0";

You won't see the double-quotes in the C program. The shell processes single and double quotes, and passes the result to the C program which is running. It might be different for built-in functions, but /bin/test would never see the double-quotes if they were used. And since the built-in function has to work the same as standalone function, I doubt the built-in would be any different.

# list_args "-n"

list_args at 19:36:15 Oct 10: $# = 1
ARG[000] l=002: '-n'
# list_args -n

list_args at 19:36:22 Oct 10: $# = 1
ARG[000] l=002: '-n'

(note that the single-quotes you see there are printed by the list_args script itself for display purposes).

/disclaimer: I think this is the first post that I've made with the new "open-source edition" of Eudora, and I have no idea if this will be formatted the way I'm expecting it be!/

--
Garance Alistair Drosehn = dro...@rpi.edu
Senior Systems Programmer or g...@freebsd.org
Rensselaer Polytechnic Institute; Troy, NY; USA

_______________________________________________
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"

Reply via email to