diff --git a/lib/filefct.sh b/lib/filefct.sh index e883892..a5c89b6 100644 --- a/lib/filefct.sh +++ b/lib/filefct.sh @@ -176,13 +176,19 @@ isdirempty() # ------------------------------------------------------------------------------ -# patch a file replacing all @var@ by the corresponding value in environment -# or the variable list given in parameter +# copy and patch a file replacing all @var@ by the corresponding value in +# environment or the variable list given in parameter patchfile() { + local srcfile=$(select_file $1) && shift local dstfile=$1 && shift 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 ( local varlist= pattern= @@ -201,15 +207,16 @@ patchfile() fi # sed replace 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" chmod ${rights[0]} "$dstfile" chown ${rights[1]}:${rights[2]} "$dstfile" - unset dstfile + unset rights dstfile } # EOF