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

Cyrille Lefevre cyrille.lefevre-lists at laposte.net
Fri Nov 5 03:18:00 UTC 2010


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 at laposte.net




More information about the freebsd-rc mailing list