Se você quiser usar sed, você pode ler de um pipe nomeado. Tenha em atenção que este código não tenta lidar com erros. O script será bloqueado se o cabeçalho do bloco dinâmico estiver presente mais de uma vez.
CONTENT_URL="http://$SERVER/get_config.php"
tmp=$(mktemp -d)
(
cd "$tmp"
mkfifo dynamic_seen dynamic_content
: >dynamic_seen & seen_pid=$!
wget -O dynamic_content "$CONTENT_URL" & wget_pid=$!
sed -e '/^# BEGIN DYNAMIC BLOCK - DO NOT EDIT MANUALLY$/ p' \
-e '/^# END DYNAMIC BLOCK$/ {'
-e p -e 'r dynamic_seen' -e 'r dynamic_content' -e '}' \
-e '/^# BEGIN DYNAMIC BLOCK - DO NOT EDIT MANUALLY$/, /^# END DYNAMIC BLOCK$/ d'
if ! kill $dynamic_seen 2>/dev/null; then
# The pipe hasn't been read, so there was no dynamic block. Add one.
echo "# BEGIN DYNAMIC BLOCK - DO NOT EDIT MANUALLY"
cat dynamic_pipe
echo "# END DYNAMIC BLOCK - DO NOT EDIT MANUALLY"
fi
)
rm -rf "$tmp"
Mas eu prefiro o awk.
export CONTENT_URL="http://$SERVER/get_config.php"
awk '
$0 == "# END DYNAMIC BLOCK - DO NOT EDIT MANUALLY" {skip=0; system("wget \"$CONTENT_URL\""); substituted=1}
!skip {print}
$0 == "# BEGIN DYNAMIC BLOCK - DO NOT EDIT MANUALLY" {skip=1}
END {
if (!substituted) {
print "# BEGIN DYNAMIC BLOCK - DO NOT EDIT MANUALLY";
system("wget \"$CONTENT_URL\"");
print "# END DYNAMIC BLOCK - DO NOT EDIT MANUALLY";
}
}
'