2021-09-05 22:07:02 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2022-01-07 23:25:09 +00:00
|
|
|
# Wrapper for 'sed -i': fail if file was not modified by sed and container was not restarted.
|
|
|
|
# Error output is surpressed, when container is restarted to avoid harmless error messages.
|
|
|
|
# Use "--strict" as first parameter, to fail regardless of the container state (fresh or restarted).
|
|
|
|
|
|
|
|
# When to use sedfile?
|
|
|
|
# Is a file change optional? --> use regular 'sed -i'
|
|
|
|
# Is a file change expected? --> use 'sedfile --strict -i'
|
|
|
|
# Is a file change only on the first container run expected? --> use 'sedfile -i'
|
2021-09-05 22:07:02 +00:00
|
|
|
|
|
|
|
set -ueo pipefail
|
|
|
|
|
|
|
|
HASHTOOL="sha1sum"
|
2022-01-07 23:25:09 +00:00
|
|
|
SKIP_ERROR=0
|
2021-09-05 22:07:02 +00:00
|
|
|
|
2022-01-07 23:25:09 +00:00
|
|
|
if [[ $# -lt 3 ]]
|
2021-09-05 22:07:02 +00:00
|
|
|
then
|
2022-01-07 23:25:09 +00:00
|
|
|
echo "Error: At least, three parameters must be given."
|
|
|
|
echo "Syntax: sedfile -i <replace/delete operation> <file>"
|
2021-09-05 22:07:02 +00:00
|
|
|
echo
|
|
|
|
exit 1
|
|
|
|
fi >&2
|
|
|
|
|
2022-01-07 23:25:09 +00:00
|
|
|
[[ -f /CONTAINER_START ]] && SKIP_ERROR=1 # Hide error, if container was restarted.
|
|
|
|
if [[ "${1}" == "--strict" ]] # Show error every time.
|
|
|
|
then
|
|
|
|
SKIP_ERROR=0
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
|
2021-09-05 22:07:02 +00:00
|
|
|
# get last argument
|
|
|
|
FILE=${*: -1}
|
|
|
|
|
|
|
|
OLD=$(${HASHTOOL} "${FILE}")
|
|
|
|
sed "$@"
|
|
|
|
NEW=$(${HASHTOOL} "${FILE}")
|
|
|
|
|
|
|
|
# fail if file was not modified
|
2022-01-07 23:25:09 +00:00
|
|
|
if [[ ${OLD} == "${NEW}" ]] && [[ ${SKIP_ERROR} -eq 0 ]]
|
2021-09-05 22:07:02 +00:00
|
|
|
then
|
|
|
|
echo "Error: sed $*"
|
|
|
|
exit 1
|
|
|
|
fi >&2
|
2022-01-07 23:25:09 +00:00
|
|
|
|
2021-09-05 22:07:02 +00:00
|
|
|
exit 0
|