Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 10 Oct 2010 16:09:54 -0700
From:      Devin Teske <dteske@vicor.com>
To:        Julian Elischer <julian@freebsd.org>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: sysrc -- a sysctl(8)-like utility for managing /etc/rc.conf et. al.
Message-ID:  <663E0B47-9318-4C75-BAEB-5C4F9EAFFDD1@vicor.com>
In-Reply-To: <4CB14E2D.3070806@freebsd.org>
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>

next in thread | previous in thread | raw e-mail | index | archive | help

On Oct 9, 2010, at 10:25 PM, Julian Elischer wrote:

> Ah grasshoppers...
>=20
> /me wonders if anyone will get the full significance of that..
>=20
>=20
> On 10/9/10 3:39 PM, Devin Teske wrote:
>> On Oct 9, 2010, at 1:25 PM, Garrett Cooper wrote:
>>=20
>>=20
>>> Why not just do...
>>>=20
>>> if [ "x$rc_conf_files" =3D x -o "x$varname" =3D x ]
>>> then
>>>    return ${FAILURE-1}
>>> fi
>> I think you'll find (quite pleasantly) that if you intonate the =
lines...
>>=20
>> 	"rc_conf_files [is non-null] OR return failure"
>> 	"varname [is non-null] OR return failure"
>>=20
>> Sounds a lot better/cleaner than the intonation of the suggested =
replacement:
>>=20
>> 	"if x plus rc_conf_files expands to something that is not equal =
to x OR x plus the expansion of varname is not x then return failure"
>>=20
>=20
>=20
> For what it matters, I'v enever found the [ "x$foo" =3D "x" ] =
construct to be useful.
> the quoting seems to work for everything I've ever worked on.

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

[ x$foo =3D x ]

is still very useful in that it prevents the error when $foo expands to =
"-e".

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

[ "$foo" =3D x ]

makes it so that the expansion is taken as:

[ "-n" =3D x ]

rather than:

[ -n =3D x ]

The former not causing an error, while the latter does.

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

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

and

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

The C-code that switch'es on the argv element can tell the difference =
between a leading quote and a leading dash and does indeed do the right =
thing for all expansions of the variable within a double-quote block =
("...").

Though, the non-quoted syntax should be avoided at all costs, imho =
unless you know for a fact that the expansion of the variable will never =
include a character from $IFS (defaults to space, tab, newline).

If it does, then it'll evaluate to a new positional argument for the =
C-code.

Take the following example:

$ foo=3D"abc =3D abc"
$ [ $foo ] && echo true
true
$ foo=3D"abc =3D 123"
$ [ $foo ] && echo true
# no output (not true)

Whereas the quoted syntax:

$ [ "$foo" ]

will always evaluate to true because there is an implied "-n" operator =
for the case where the first-and-last positional argument is a =
double-quoted string, and...

$ [ -n "$foo" ]

always evaluates to true in the above cases for foo ("abc =3D abc" then =
later "abc =3D 123").




> ...
>=20
> Now One thing that should be bourne in mind (heh) is that
> as there is a 'usual' form of format for perl there is one for sh as =
well so it's not "polite"
> to make one's sh code look like perl.  :-)

Perusing sh(1) today... found examples of the lazy operators:

[ expr ] || expr
[ expr ] && expr

Search under "Short-Circuit List Operators"

I'd say that the description therein is a green-light to treat sh like =
perl ^_^


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

--
Cheers,
Devin Teske

-> CONTACT INFORMATION <-
Business Solutions Consultant II
FIS - fisglobal.com
510-735-5650 Mobile
510-621-2038 Office
510-621-2020 Office Fax
909-477-4578 Home/Fax
devin.teske@fisglobal.com

-> LEGAL DISCLAIMER <-
This message  contains confidential  and proprietary  information
of the sender,  and is intended only for the person(s) to whom it
is addressed. Any use, distribution, copying or disclosure by any
other person  is strictly prohibited.  If you have  received this
message in error,  please notify  the e-mail sender  immediately,
and delete the original message without making a copy.

-> FUN STUFF <-
-----BEGIN GEEK CODE BLOCK-----
Version 3.1
GAT/CS d(+) s: a- C++(++++) UB++++$ P++(++++) L++(++++) !E--- W++ N? o? =
K- w O
M+ V- PS+ PE Y+ PGP- t(+) 5? X+(++) R>++ tv(+) b+(++) DI+(++) D(+) G+>++ =
e>+ h
r>++ y+=20
------END GEEK CODE BLOCK------
http://www.geekcode.com/

-> END TRANSMISSION <-




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?663E0B47-9318-4C75-BAEB-5C4F9EAFFDD1>