Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 11 May 2015 12:49:58 -0700
From:      Devin Teske <dteske@FreeBSD.org>
To:        Eric van Gyzen <vangyzen@FreeBSD.org>
Cc:        Brooks Davis <brooks@freebsd.org>, John Baldwin <jhb@FreeBSD.org>, Xin LI <delphij@FreeBSD.org>, "src-committers@freebsd.org" <src-committers@freebsd.org>, "svn-src-all@freebsd.org" <svn-src-all@freebsd.org>, "svn-src-head@freebsd.org" <svn-src-head@freebsd.org>, Devin Teske <dteske@FreeBSD.org>
Subject:   Re: svn commit: r282672 - head/etc/rc.d
Message-ID:  <5AE2B9A2-FF6E-422A-B2E2-7E6CA85999BD@FreeBSD.org>
In-Reply-To: <555106B5.3010104@FreeBSD.org>
References:  <201505082336.t48NaWRS080408@svn.freebsd.org> <C072FED2-FBED-4EF9-9D74-11B485594AD7@FreeBSD.org> <20150511191850.GC68045@spindle.one-eyed-alien.net> <9FB76653-EF89-48E5-B4E8-A2006923B76A@FreeBSD.org> <555106B5.3010104@FreeBSD.org>

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


> On May 11, 2015, at 12:44 PM, Eric van Gyzen <vangyzen@FreeBSD.org> wrote:
> 
> On 05/11/2015 15:37, Devin Teske wrote:
>> 
>>> On May 11, 2015, at 12:18 PM, Brooks Davis <brooks@freebsd.org <mailto:brooks@freebsd.org>> wrote:
>>> 
>>> On Sun, May 10, 2015 at 03:45:48PM -0400, John Baldwin wrote:
>>>> 
>>>>> On May 8, 2015, at 19:36, Xin LI <delphij@FreeBSD.org <mailto:delphij@FreeBSD.org>> wrote:
>>>>> 
>>>>> Author: delphij
>>>>> Date: Fri May  8 23:36:31 2015
>>>>> New Revision: 282672
>>>>> URL: https://svnweb.freebsd.org/changeset/base/282672
>>>>> 
>>>>> Log:
>>>>> Always convert uuid to lower case.
>>>>> 
>>>>> MFC after:    2 weeks
>>>>> 
>>>>> Modified:
>>>>> head/etc/rc.d/hostid
>>>>> 
>>>>> Modified: head/etc/rc.d/hostid
>>>>> ==============================================================================
>>>>> --- head/etc/rc.d/hostid    Fri May  8 23:29:42 2015    (r282671)
>>>>> +++ head/etc/rc.d/hostid    Fri May  8 23:36:31 2015    (r282672)
>>>>> @@ -58,7 +58,7 @@ hostid_set()
>>>>> 
>>>>> valid_hostid()
>>>>> {
>>>>> -    uuid=$1
>>>>> +    uuid=$(echo $1 | tr '[:upper:]' '[:lower:]')
>>>> 
>>>> tr is in /usr/bin so this breaks systems with a separate /usr.  Perhaps you could use dd with conv=lcase instead?
>>> 
>>> Alterntively, a shell function "ltr" exists in rc.subr for this purpose.
>>> 
>> 
>> ltr would not work in this situation, for multiple reasons.
>> 
>> 1. ltr doesn’t support character classes
>> 2. ltr is for replacing one or more characters (cannot be a class) with a single string (of variable length, 0+).
>> 
>> In /etc/networks.subr you can see an example usage of ltr:
>> 
>> 287 <https://svnweb.freebsd.org/base/head/etc/network.subr?view=markup#l287>;	_punct=".-/+"
>> 288 <https://svnweb.freebsd.org/base/head/etc/network.subr?view=markup#l288>;	ltr ${_if} "${_punct}" '_' _if
>> 
>> 
>> The result of this is to take a value of (for example) foo.bar and replace
>> any occurrences of period, minus, forward slash, or plus with instead
>> a single underscore. The result is stuffed into the variable “_if” (over-
>> writing previous contents which may have contained aforementioned
>> characters replaced with underscore).
>> 
>> An attempt to use ltr in the below fashion:
>> 
>> ltr $string ‘[:lower:]’ ‘[:upper:]’ somevar
>> 
>> would surely fail.
>> 
>> While it is indeed *possible* to write a find/replace function in native-
>> shell that supports character classes, it would not be a small function.
>> The primary issue is that you need to know what the character that
>> matched the class and there aren’t any built-ins that provide this info.
>> 
>> For example:
>> 
>> case “$src” in *[[:lower:]]*)
>> 
>> will trigger when you have a lower-case character that needs conversion
>> to upper-case (or opposite if using *[[:upper:]]*) BUT you won’t know
>> what the character was that you matched (so how can you know which
>> upper-case character to supplant)?
>> 
>> The function will have to resort to complicated substring mechanics or
>> any other seldom known procedure.
>> 
>> I’ll have a noodle on it and see what I can come up with. It’s not exactly
>> immediately coming to me how to do this in any simple fashion while
>> maintaining efficiency (read: by not iterating over every single character
>> and also by not having a giant massive case statement with every letter
>> spelled out — coming up with a solution that embraces the use of the
>> character class I would believe to be more efficient).
> 
> John Baldwin suggested "dd conv=lcase", since dd is in /bin.
> 
> $ echo MixedCaseLetters | dd conv=lcase 2>/dev/null
> mixedcaseletters
> 
> $ type dd
> dd is /bin/dd
> 

Yeah, I have to say, this looks by far the simplest approach.
Even if I or Colin or someone else were to whip up a comprehensive
native shell function that does this, I can’t say that it would be chosen
necessarily over the dd approach (which itself is comprehensive but
may suffer the lag of pulling dd into memory — most significant when
say, running off of CD). Not knowing where the code is destined nor
how many times iteratively it could be passed over in a casual day,
it’s still probably likely that the dd approach is the best (unless you’re
doing it repetitively in a tight loop ;D).
— 
Devin




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?5AE2B9A2-FF6E-422A-B2E2-7E6CA85999BD>