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 =E9crit :

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

# no $(cat << EOF needed here, so, no extra \ and $quot and so needed
awkscript=3D'
# %s/\\$0/$0/;s/\\\\/\\/g
	BEGIN { ...; regex=3D"^[[:space:]]*" varname "=3D" }
=2E..
	if ( ! match($0, regex) ) { print; next }
=2E..
	if ( t1 ~ /[\'\$\\]/ )
=2E..
	else if ( t1 =3D=3D apos ) {
		sub("^" apos "[^" apos "]*", "", value)
		if ( length(value) =3D=3D 0 ) t2 =3D ""
		sub("^" apos, "", value)
=2E..
	else if ( t1 =3D=3D "\"" ) {
		sub(/^"[^"]*/, "", value)
		if ( length(value) =3D=3D 0 ) t2 =3D ""
		sub(/^"/, "", value)
=2E..
		t1 =3D t2 =3D "\""
=2E..
		else if ( t1 ~ /[[:space:]];#]/ )
# parentheses aren't needed here, or wrap them as before
			t1 =3D t2 =3D "\""
=2E..
	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=3D"$( echo "$new_value" |
	awk '{ gsub(/\\/, "\\\\"); gsub(/"/, "\\\""); print }' )"
=2E..
# you missed the " here
	new_contents=3D$( 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=3D$( echo "$new_contents" |
			awk -v varname=3D"$varname" -v apos=3D"'" \
			    -v new_value=3D"$new_value" "$awkscript")

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

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

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

Regards,

Cyrille Lefevre
--=20
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>