filefct.sh: better implementation of patchfile

This commit is contained in:
levasseur
2021-10-26 18:13:57 +02:00
parent 6fa6dcc24d
commit 9b8f55130b

View File

@@ -176,13 +176,19 @@ isdirempty()
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# patch a file replacing all @var@ by the corresponding value in environment # copy and patch a file replacing all @var@ by the corresponding value in
# or the variable list given in parameter # environment or the variable list given in parameter
patchfile() patchfile()
{ {
local srcfile=$(select_file $1) && shift
local dstfile=$1 && shift local dstfile=$1 && shift
local workfile=${dstfile}.work local workfile=${dstfile}.work
if [[ ! -s $srcfile ]]; then
prnt E "Le fichier source est vide, n'est pas un fichier ou n'existe pas"
die 10
fi
# Create a sub-process, to avoid bash environment pollution # Create a sub-process, to avoid bash environment pollution
( (
local varlist= pattern= local varlist= pattern=
@@ -201,15 +207,16 @@ patchfile()
fi fi
# sed replace <VAR> with \$$VAR and envsubst do the replace by value # sed replace <VAR> with \$$VAR and envsubst do the replace by value
sed $pattern $dstfile | envsubst ${varlist:+"$varlist"} > "$workfile" sed $pattern $srcfile | envsubst ${varlist:+"$varlist"} > "$workfile"
) )
local -a rights=( $(stat --printf="%a %u %g" "$dstfile") ) local -a rights=( $(stat --printf="%a %u %g" "$srcfile") )
unset srcfile
mv "$workfile" "$dstfile" mv "$workfile" "$dstfile"
chmod ${rights[0]} "$dstfile" chmod ${rights[0]} "$dstfile"
chown ${rights[1]}:${rights[2]} "$dstfile" chown ${rights[1]}:${rights[2]} "$dstfile"
unset dstfile unset rights dstfile
} }
# EOF # EOF