Date: Sun, 10 Oct 2010 19:51:52 -0400 From: Garance A Drosihn <drosih@rpi.edu> To: Devin Teske <dteske@vicor.com> Cc: freebsd-hackers@freebsd.org Subject: Re: sysrc -- a sysctl(8)-like utility for managing /etc/rc.conf et. al. Message-ID: <4CB25198.4020209@rpi.edu> In-Reply-To: <663E0B47-9318-4C75-BAEB-5C4F9EAFFDD1@vicor.com> References: <1286397912.27308.40.camel@localhost.localdomain> <AANLkTikoohMo5ng-RM3tctTH__P6cqhQpm=FPhSE9mMg@mail.gmail.com> <51B4504F-5AA4-47C5-BF23-FA51DE5BC8C8@vicor.com> <AANLkTim=BLkd229vdEst8U0ugpq3UsHPxjZZp2qaJxH-@mail.gmail.com> <238E0B24-AA12-4684-9651-84DA665BE893@vicor.com> <4CB14E2D.3070806@freebsd.org> <663E0B47-9318-4C75-BAEB-5C4F9EAFFDD1@vicor.com>
next in thread | previous in thread | raw e-mail | index | archive | help
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 = drosih@rpi.edu Senior Systems Programmer or gad@FreeBSD.org Rensselaer Polytechnic Institute; Troy, NY; USA
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4CB25198.4020209>