Date: Sat, 06 Nov 2010 02:32:48 +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: <4CD4B040.1000804@laposte.net> In-Reply-To: <1288978858.7362.154.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> <4CD3731C.6020501@laposte.net> <1288978858.7362.154.camel@localhost.localdomain>
next in thread | previous in thread | raw e-mail | index | archive | help
Le 05/11/2010 18:40, Devin Teske a =E9crit : > On Fri, 2010-11-05 at 03:59 +0100, Cyrille Lefevre wrote: >> Le 05/11/2010 02:09, Devin Teske a =E9crit : 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=3D' >> # %s/\\$0/$0/;s/\\\\/\\/g >> BEGIN { ...; regex=3D"^[[:space:]]*" varname "=3D" } >=20 > Won't work. >=20 > 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=3D"$varname" ... "$awkscript" > So right off the bat, your awk script will fail because regex will be > assigned a value of: >=20 > ^[[:space:]]*=3D wrong too, $ v=3D123; awk -v v=3D"$v" 'BEGIN{ r=3D"^[[:space:]]*"v"=3D"; print r; ex= it}' ^[[:space:]]*123=3D >> ... >> if ( t1 ~ /[\'\$\\]/ ) >=20 > Since we're picking nits: > - the backslash before the apostrophe is not needed. right :-) >> ... >> else if ( t1 =3D=3D apos ) { >=20 > 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 (somethin= g > that's not really important for the awkscript itself, but it's rather > disconcerting to see entire oceans of miscolored text AFTER the embedde= d > here-document). done in awk -v apos=3D"'" ... "$awkscript" below >> ... >> else if ( t1 ~ /[[:space:]];#]/ ) >> # parentheses aren't needed here, or wrap them as before >> t1 =3D t2 =3D "\"" bad copy/paste, I was talking about the later one (null assignment) # Null-assignment else if ( t1 ~ /[[:space:]];#]/ ) { t1 =3D t2 =3D "$bquot" } should be # Null-assignment else if ( t1 ~ /[[:space:]];#]/ ) { t1 =3D t2 =3D "$bquot" } to look like above syntax. >> ... >> printf "%s%c%s%c%s\n", substr(\$0, 0, matchlen), \ >> t1, awk_new_value, t2, value >> ' >=20 > 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=3D"$( echo "$new_value" | >> awk '{ gsub(/\\/, "\\\\"); gsub(/"/, "\\\""); print }' )" >=20 > Wrong. Fail. And here's why... >=20 > You are correct that a $( ... ) block can traverse newlines. >=20 > However, what $( ... ) functional performs is a sub-shell. Each line > within the $( ... ) syntax is taken as a single-line of shell to be > executed. >=20 > 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=3D$(echo x \ | sed s,x,y,) echo $x the usual syntax : x=3D$(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=3D$( tail -r "$file" 2> /dev/null ) >=20 > 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=3D$( echo "$new_contents" | >> awk -v varname=3D"$varname" -v apos=3D"'" \ >> -v new_value=3D"$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=3D/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 --=20 mailto:Cyrille.Lefevre-lists@laposte.net
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4CD4B040.1000804>