filefct.sh: better implementation of patchfile
This commit is contained in:
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user