sysrc(8) -- a sysctl(8)-like utility for managing rc.conf(5)

Cyrille Lefevre cyrille.lefevre-lists at laposte.net
Sat Nov 6 01:30:32 UTC 2010


Le 05/11/2010 18:40, Devin Teske a écrit :
> On Fri, 2010-11-05 at 03:59 +0100, Cyrille Lefevre wrote:
>> Le 05/11/2010 02:09, Devin Teske a écrit :

you don't have to comment every line w/ 10+ lines of comments.
I don't agress you, it was just a proposal which is IMHO more
readable. however, it's your code and you make whatever you want
w/ it. the more important thing is that you understand it, well,
except if someone else have to maintain it also :-)

>> awkscript='
>> # %s/\\$0/$0/;s/\\\\/\\/g
>> 	BEGIN { ...; regex="^[[:space:]]*" varname "=" }
> 
> Won't work.
> 
> The point of failure here is that you've taken $varname (which is a
> positional parameter passed to the shell script function) and turned it
> into an awk variable (which is never assigned).

you're wrong, the assignment is done later in :
awk -v varname="$varname" ... "$awkscript"

> So right off the bat, your awk script will fail because regex will be
> assigned a value of:
> 
> ^[[:space:]]*=

wrong too,
$ v=123; awk -v v="$v" 'BEGIN{ r="^[[:space:]]*"v"="; print r; exit}'
^[[:space:]]*123=

>> ...
>> 	if ( t1 ~ /[\'\$\\]/ )
> 
> Since we're picking nits:
> - the backslash before the apostrophe is not needed.

right :-)

>> ...
>> 	else if ( t1 == apos ) {
> 
> apos needs to be a shell-expanded parameter/variable, otherwise you'll
> be forced to declare apos in the awkscript, which itself (without using
> compound strings) will force vim syntax highlighting to break (something
> that's not really important for the awkscript itself, but it's rather
> disconcerting to see entire oceans of miscolored text AFTER the embedded
> here-document).

done in awk -v apos="'" ... "$awkscript" below

>> ...
>> 		else if ( t1 ~ /[[:space:]];#]/ )
>> # parentheses aren't needed here, or wrap them as before
>> 			t1 = t2 = "\""

bad copy/paste, I was talking about the later one (null assignment)

		# Null-assignment
		else if ( t1 ~ /[[:space:]];#]/ )
		{ t1 = t2 = "$bquot" }
should be
		# Null-assignment
		else if ( t1 ~ /[[:space:]];#]/ ) {
			t1 = t2 = "$bquot"
		}
to look like above syntax.

>> ...
>> 	printf "%s%c%s%c%s\n", substr(\$0, 0, matchlen), \
>> 		t1, awk_new_value, t2, value
>> '
> 
> Just as before with $regex (which had $varname which is $1 which is the
> positional parameter passed to the shell function), this will fail
> because awk_new_value is not defined by awk.

see below too ;^)

> Further, you've forgotten to conver \$0 to $0 (considering that you've

no, look at the # %s/\\$0/$0/;s/\\\\/\\/g abobe

>> # ... | ... doesn't need a final \ when wrapped after the |
>> local awk_new_value="$( echo "$new_value" |
>> 	awk '{ gsub(/\\/, "\\\\"); gsub(/"/, "\\\""); print }' )"
> 
> Wrong. Fail. And here's why...
> 
> You are correct that a $( ... ) block can traverse newlines.
> 
> However, what $( ... ) functional performs is a sub-shell. Each line
> within the $( ... ) syntax is taken as a single-line of shell to be
> executed.
> 
> Therefore, by deleting the back-slash at the end of the line, you've
> turned one statement into two.

no, no, no, did you tried it ?

your syntax :
x=$(echo x \
  | sed s,x,y,)
echo $x

the usual syntax :
x=$(echo x |
    sed s,x,y,)
echo $x

both should print y or the shell is buggy ?

so, as I told you, the \ isn't needed after a |.

>> # you missed the " here
>> 	new_contents=$( tail -r "$file" 2> /dev/null )
> 
> No, I did not. And here's why...

I was talking about the ones around $file, not the ones around $()

>> # you may want to use printf "%s" "$new_contents" instead of echo
>> # to avoid \ sequences interpretation if any
>> 	new_contents=$( echo "$new_contents" |
>> 			awk -v varname="$varname" -v apos="'" \
>> 			    -v new_value="$new_value" "$awkscript")

when I'll have time, I'll try my code against your code to see the
differences and if there is so many bad effects ?

>> also, %s|/bin/sh|$_PATH_BSHELL| && _PATH_BSHELL=/bin/sh

it's just an habit to hardcode path within variables and a proposal.

> Not sure where you got the bquotquot (not in my code).

in my example, the %s/quot/dquot/ statment turns bquot to bquotquot :-)

> perl(1)). You cannot perform multiple '%s///' operators separated by a
> semi-colon. You would get the error "E488: Trailing characters" in vim

it was just a synthetic form of writing, no more.

Regards,

Cyrille Lefevre
-- 
mailto:Cyrille.Lefevre-lists at laposte.net




More information about the freebsd-rc mailing list