Thanks Noah. It was working for me when I piped the output to /etc/hosts2, but shame on me for not testing it all the way through. I also escaped the space, but my blogging software must have munched it.
Perhaps the pithiness of the original could be retained if it were ported to Ruby.
Fnor
– 2007-07-20T10:00:06Z
You can also use the -i option of sed to edit in place instead of redirecting to a temporary file : )
Noah Slater
– 2007-07-20T10:58:39Z
Aha, but the "-i" option is not portable. Heh.
Noah Slater
– 2007-07-20T11:45:25Z
Aha, but the "-i" option is not portable. Heh.
Noah Slater
– 2007-07-20T11:46:22Z
Your blogging software should do a redirect after the POST to avoid duplicate submissions by pressing "refresh".
Fnor
– 2007-07-20T12:22:32Z
You can also use the -i option of sed to edit in place instead of redirecting to a temporary file : )
Firstly, you need to quote the argument to sed or you will get a "sed: -e expression #1, char 21: unterminated `s' command" error.
Secondly, you cannot use sed to read from a while while concurrently writing to the same file. You need to use a temporary file in this instance.
A working script would look something like:
temporary_file=$(tempfile)
if [ $? -ne 0 ]; then
echo "Could not create temporary file.";
else
sed "s/localhost/localhost lolcathost/" /etc/hosts > $temporary_file;
mv $temporary_file /etc/hosts
fi
Perhaps the pithiness of the original could be retained if it were ported to Ruby.
And thanks for the portability information.
Updated the example; thanks.