Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 05 Nov 2010 03:59:40 +0100
From:      Cyrille Lefevre <cyrille.lefevre-lists@laposte.net>
To:        Devin Teske <dteske@vicor.com>
Cc:        freebsd-rc@freebsd.org
Subject:   Re: sysrc(8) -- a sysctl(8)-like utility for managing rc.conf(5)
Message-ID:  <4CD3731C.6020501@laposte.net>
In-Reply-To: <1288919368.7362.35.camel@localhost.localdomain>
References:  <1286925182.32724.18.camel@localhost.localdomain>	<1286996709.32724.60.camel@localhost.localdomain>	<1287448781.5713.3.camel@localhost.localdomain>	<1287510629.25599.2.camel@localhost.localdomain>	<D763F474-8F19-4C65-B23F-78C9B137A8FE@vicor.com>	<1288746388.7362.4.camel@localhost.localdomain>	<17B64023-A64A-40DA-9CBC-A601710AB5BB@vicor.com> <1288919368.7362.35.camel@localhost.localdomain>

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


Le 05/11/2010 02:09, Devin Teske a écrit :

hi, how about something like (untested, but should work) :

# no $(cat << EOF needed here, so, no extra \ and $quot and so needed
awkscript='
# %s/\\$0/$0/;s/\\\\/\\/g
	BEGIN { ...; regex="^[[:space:]]*" varname "=" }
...
	if ( ! match($0, regex) ) { print; next }
...
	if ( t1 ~ /[\'\$\\]/ )
...
	else if ( t1 == apos ) {
		sub("^" apos "[^" apos "]*", "", value)
		if ( length(value) == 0 ) t2 = ""
		sub("^" apos, "", value)
...
	else if ( t1 == "\"" ) {
		sub(/^"[^"]*/, "", value)
		if ( length(value) == 0 ) t2 = ""
		sub(/^"/, "", value)
...
		t1 = t2 = "\""
...
		else if ( t1 ~ /[[:space:]];#]/ )
# parentheses aren't needed here, or wrap them as before
			t1 = t2 = "\""
...
	printf "%s%c%s%c%s\n", substr(\$0, 0, matchlen), \
		t1, awk_new_value, t2, value
'

# ... | ... doesn't need a final \ when wrapped after the |
local awk_new_value="$( echo "$new_value" |
	awk '{ gsub(/\\/, "\\\\"); gsub(/"/, "\\\""); print }' )"
...
# you missed the " here
	new_contents=$( tail -r "$file" 2> /dev/null )
# 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")

of course, same remarks about the later awk script :-)

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

PS : for non french user :
%s/quot/dquot/;%s/apos/squot/;s/bquotquot/bquot/

Regards,

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





Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4CD3731C.6020501>