filefct.sh: few fixes after last commit

This commit is contained in:
2022-01-27 19:57:38 +01:00
parent 55a9e49101
commit 6131afa3e3

View File

@@ -15,7 +15,7 @@
backup_dist() backup_dist()
{ {
if [[ $# -lt 1 ]]; then if [[ $# -lt 1 ]]; then
prnt E "backupdist(): At least one argument is required." prnt E "backup_dist(): At least one argument is required."
exit 11 exit 11
fi fi
@@ -23,31 +23,31 @@ backup_dist()
for file in $@; do for file in $@; do
local tmstmp=$(stdtime) local tmstmp=$(stdtime)
if [[ -L ${file} ]]; then if [[ -L ${file} ]]; then
# With symbolik links we call again backupdist to treat target # With symbolik links we call again backup_dist to treat target
prnt I "Following the symbolic link $file to do a proper backup..." prnt I "Following the symbolic link $file to do a proper backup..."
backupdist $(readlink -f ${file}) backup_dist $(readlink -f ${file})
elif [[ -f ${file} ]]; then elif [[ -f ${file} ]]; then
prnt I "Creating a backup of ${file} on $tmstmp..." prnt I "Creating a backup of ${file} on $tmstmp..."
cp -av $file ${file}.dist.${tmstmp} cp -av $file ${file}.dist.${tmstmp}
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
prnt E "backupdist(): Failed copying file." prnt E "backup_dist(): Failed copying file."
die 12 die 12
fi fi
elif [[ -d ${file} ]]; then elif [[ -d ${file} ]]; then
prnt I "Creation a backup of the directory ${file} on $tmstmp..." prnt I "Creation a backup of the directory ${file} on $tmstmp..."
cp -av $file ${file}.dist.${tmstmp} cp -av $file ${file}.dist.${tmstmp}
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
prnt E "backupdist(): Failed copyind directory recursively." prnt E "backup_dist(): Failed copyind directory recursively."
die 12 die 12
fi fi
else else
prnt W "backupdist(): $file don't exists, nothing to do." prnt W "backup_dist(): $file don't exists, nothing to do."
fi fi
unset tmstmp unset tmstmp
done done
unset file unset file
} }
export -f backupdist export -f backup_dist
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
@@ -67,6 +67,7 @@ select_file()
echo $source echo $source
unset source unset source
} }
export -f select_file
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
@@ -86,6 +87,7 @@ select_directory()
echo $source echo $source
unset source unset source
} }
export -f select_directory
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
@@ -97,11 +99,11 @@ install_file()
local i=0 local i=0
if [[ $# -lt 2 ]]; then if [[ $# -lt 2 ]]; then
prnt E "installfile(): At least two arguments are required." prnt E "install_file(): At least two arguments are required."
die 11 die 11
fi fi
if [[ $(echo $@ | grep "\*\|\?") ]]; then if [[ $(echo $@ | grep "\*\|\?") ]]; then
prnt E "installfile(): Wildcards are not authorized." prnt E "install_file(): Wildcards are not authorized."
die 7 die 7
fi fi
@@ -117,7 +119,7 @@ install_file()
: :
done done
if [[ ! $file == /* ]]; then if [[ ! $file == /* ]]; then
prnt E "installfile(): Target must be on the root filesystem and full path must be provided." prnt E "install_file(): Target must be on the root filesystem and full path must be provided."
die 13 die 13
fi fi
unset file unset file
@@ -126,18 +128,18 @@ install_file()
prnt I "Creating required target directory $(dirname $i)..." prnt I "Creating required target directory $(dirname $i)..."
mkdir -pv $(dirname $i) mkdir -pv $(dirname $i)
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
prnt E "installfile(): Can't create target directory!" prnt E "install_file(): Can't create target directory!"
die 12 die 12
fi fi
fi fi
prnt I "Copying files ${filelist} to target directory $(dirname $i)..." prnt I "Copying files ${filelist} to target directory $(dirname $i)..."
cp -av $filelist cp -av $filelist
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
prnt E "installfile(): Couldn't copy some required files!" prnt E "install_file(): Couldn't copy some required files!"
die 12 die 12
fi fi
} }
export -f installfile export -f install_file
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
@@ -147,22 +149,22 @@ append_file()
local srcfile=$(select_file $1) local srcfile=$(select_file $1)
local dstfile=$2 local dstfile=$2
if [[ -e $dstfile ]]; then if [[ -e $dstfile ]]; then
prnt E "appendfile(): Target must be on the root filesystem and full path must be provided." prnt E "append_file(): Target must be on the root filesystem and full path must be provided."
die 13 die 13
fi fi
if [[ ! $dstfile == /* ]]; then if [[ ! $dstfile == /* ]]; then
prnt E "appendfile(): Target file must exist." prnt E "append_file(): Target file must exist."
die 13 die 13
fi fi
prnt I "Adding content to file $dstfile..." prnt I "Adding content to file $dstfile..."
cat $srcfile >> $dstfile cat $srcfile >> $dstfile
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
prnt E "appendfile(): Couldn't append a file!" prnt E "append_file(): Couldn't append a file!"
die 12 die 12
fi fi
} }
export -f appendfile export -f append_file
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
@@ -172,7 +174,7 @@ is_dir_empty()
dir=$1 dir=$1
if [[ -f $dir ]]; then if [[ -f $dir ]]; then
prnt E "isdirempty(): The given parameter is not a directory." prnt E "is_dir_empty(): The given parameter is not a directory."
die 15 die 15
fi fi
if [[ ! -d $dir ]]; then if [[ ! -d $dir ]]; then
@@ -185,7 +187,7 @@ is_dir_empty()
fi fi
return 1 return 1
} }
export -f isdirempty export -f is_dir_empty
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
@@ -198,7 +200,7 @@ patch_file()
local workfile=${dstfile}.work local workfile=${dstfile}.work
if [[ ! -s $srcfile ]]; then if [[ ! -s $srcfile ]]; then
prnt E "patchfile(): Source file is empty, is not a file or don't exists!" prnt E "patch_file(): Source file is empty, is not a file or don't exists!"
die 10 die 10
fi fi
@@ -231,7 +233,7 @@ patch_file()
unset rights dstfile unset rights dstfile
} }
export -f patchfile export -f patch_file
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
@@ -248,7 +250,7 @@ tag_file()
fi fi
done done
} }
export -f tagfile export -f tag_file
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------