From owner-freebsd-hackers@FreeBSD.ORG Mon Oct 11 00:59:57 2010 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 9B2141065673; Mon, 11 Oct 2010 00:59:57 +0000 (UTC) (envelope-from drosih@rpi.edu) Received: from smtp5.server.rpi.edu (smtp5.server.rpi.edu [128.113.2.225]) by mx1.freebsd.org (Postfix) with ESMTP id 4D7118FC14; Mon, 11 Oct 2010 00:59:57 +0000 (UTC) Received: from gilead.netel.rpi.edu (gilead.netel.rpi.edu [128.113.124.121]) by smtp5.server.rpi.edu (8.13.1/8.13.1) with ESMTP id o9ANpqwI001556; Sun, 10 Oct 2010 19:51:53 -0400 Message-ID: <4CB25198.4020209@rpi.edu> Date: Sun, 10 Oct 2010 19:51:52 -0400 From: Garance A Drosihn User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.9) Gecko/20100722 Eudora/3.0.4 MIME-Version: 1.0 To: Devin Teske References: <1286397912.27308.40.camel@localhost.localdomain> <51B4504F-5AA4-47C5-BF23-FA51DE5BC8C8@vicor.com> <238E0B24-AA12-4684-9651-84DA665BE893@vicor.com> <4CB14E2D.3070806@freebsd.org> <663E0B47-9318-4C75-BAEB-5C4F9EAFFDD1@vicor.com> In-Reply-To: <663E0B47-9318-4C75-BAEB-5C4F9EAFFDD1@vicor.com> X-Bayes-Prob: 0.0001 (Score 0) X-RPI-SA-Score: 2.60 (**) [Hold at 15.00] HTML_MESSAGE, J_CHICKENPOX_13, J_CHICKENPOX_14, RATWARE_GECKO_BUILD, 22490(-25) X-CanItPRO-Stream: outgoing X-Canit-Stats-ID: Bayes signature not available X-Scanned-By: CanIt (www . roaringpenguin . com) on 128.113.2.225 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Content-Filtered-By: Mailman/MimeDel 2.1.5 Cc: freebsd-hackers@freebsd.org Subject: Re: sysrc -- a sysctl(8)-like utility for managing /etc/rc.conf et. al. X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 11 Oct 2010 00:59:57 -0000 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