著者: Rainer Weikusat 日付: To: dng 題目: Re: [DNG] netman-autostart_0.1.1
Edward Bartolo <edbarx@???> writes: > sed -i "/Comment=/c\ ${comment}" "${launcher}"
>
> Hi Rainer et al,
>
> Using the above command results in replacing lines containing
> "Comment=" by the value of launcher plus one leading space. The weird
> thing is that I found that if I remove the space between
> "/Comment=/c\" and "${comment}", the line will be replaced by the text
> "${Comment}". I am attributing this to a possible shell bug.
Assuming comment is "tnemmoc", running
"/Comment=/c\ ${comment}"
through the shell will result in the string
/Comment=/c tnemmoc
as the shell interprets the \ as "escape the next character" and
escaping a blank is a no-op. If the string's
"/Comment=/c\${comment}"
the $ will be escaped, hence, no interpolation is done and the resulting
string becomes
/Comment=/c${comment}
sed -i "/Comment=/c$comment"
works as intended.
NB: Despite GNU sed has meanwhile gained transparent support for the
usual temporary-file-and-rename hacks, it's still the stream editor and
hence, restricted to one-pass algorithms and the UNIX(*) line-editor is
still called ed.
A one-line removing all lines starting with Comment= followed by adding
the content of the variable comm could be:
printf 'g/^Comment=/d\na\n%s\n.\nwq\n' "$comment" | ed netman.desktop
This uses printf to do the interpolation so that the template string
can be quoted with ' and hence, the shell won't mess with its contents.