correctly export array to subscript

* fix shellcheck errors and warnings
This commit is contained in:
Martin Zuther 2019-12-28 16:09:19 +01:00
parent 0b79b461a6
commit c190333fdf
No known key found for this signature in database
GPG Key ID: 47D3346D758A37D8
3 changed files with 43 additions and 17 deletions

View File

@ -17,9 +17,16 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
YADM_DIRECTORY=$(dirname $YADM_HOOK_REPO) YADM_DIRECTORY=$(dirname "$YADM_HOOK_REPO")
YADM_CHECKSUMS=$YADM_DIRECTORY/files.checksums YADM_CHECKSUMS=$YADM_DIRECTORY/files.checksums
# unpack exported array; filenames including a newline character (\n)
# are NOT supported
OLD_IFS="$IFS"
IFS=$'\n'
YADM_ENCRYPT_INCLUDE_FILES=( $YADM_ENCRYPT_INCLUDE_FILES )
IFS="$OLD_IFS"
CHECKSUM_ALGORITHM="512" CHECKSUM_ALGORITHM="512"
CHECKSUM_ALGORITHM_NAME="SHA-512" CHECKSUM_ALGORITHM_NAME="SHA-512"
WARNING_MESSAGE="No checksums were created." WARNING_MESSAGE="No checksums were created."
@ -35,7 +42,7 @@ function print_warning_and_exit {
# reset output color # reset output color
echo -e "\033[0m" echo -e "\033[0m"
exit $YADM_HOOK_EXIT exit "$YADM_HOOK_EXIT"
} }
@ -50,7 +57,7 @@ function ensure_command {
function ensure_algorithm { function ensure_algorithm {
# check if "shasum" supports algorithm # check if "shasum" supports algorithm by hashing an empty string
echo -n | shasum --algorithm "$CHECKSUM_ALGORITHM" &> /dev/null echo -n | shasum --algorithm "$CHECKSUM_ALGORITHM" &> /dev/null
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
@ -64,11 +71,11 @@ ensure_command shasum
ensure_algorithm ensure_algorithm
# empty checksum file # empty checksum file
echo -n > $YADM_CHECKSUMS echo -n > "$YADM_CHECKSUMS"
# calculate checksums for encrypted files # calculate checksums for encrypted files
for included in "${YADM_ENCRYPT_INCLUDE_FILES[@]}"; do for included in ${YADM_ENCRYPT_INCLUDE_FILES[*]}; do
shasum --algorithm $CHECKSUM_ALGORITHM $included >> $YADM_CHECKSUMS shasum --algorithm $CHECKSUM_ALGORITHM "$included" >> "$YADM_CHECKSUMS"
# signal errors # signal errors
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
@ -79,4 +86,4 @@ done
echo "Wrote checksums: $YADM_CHECKSUMS ($CHECKSUM_ALGORITHM_NAME)" echo "Wrote checksums: $YADM_CHECKSUMS ($CHECKSUM_ALGORITHM_NAME)"
# return exit status of the yadm command # return exit status of the yadm command
exit $YADM_HOOK_EXIT exit "$YADM_HOOK_EXIT"

View File

@ -17,9 +17,16 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
YADM_DIRECTORY=$(dirname $YADM_HOOK_REPO) YADM_DIRECTORY=$(dirname "$YADM_HOOK_REPO")
YADM_CHECKSUMS=$YADM_DIRECTORY/files.checksums YADM_CHECKSUMS=$YADM_DIRECTORY/files.checksums
# unpack exported array; filenames including a newline character (\n)
# are NOT supported
OLD_IFS="$IFS"
IFS=$'\n'
YADM_ENCRYPT_INCLUDE_FILES=( $YADM_ENCRYPT_INCLUDE_FILES )
IFS="$OLD_IFS"
CHECKSUM_ALGORITHM="512" CHECKSUM_ALGORITHM="512"
CHECKSUM_ALGORITHM_NAME="SHA-512" CHECKSUM_ALGORITHM_NAME="SHA-512"
WARNING_MESSAGE="Checksums were not verified." WARNING_MESSAGE="Checksums were not verified."
@ -35,7 +42,7 @@ function print_warning_and_exit {
# reset output color # reset output color
echo -e "\033[0m" echo -e "\033[0m"
exit $YADM_HOOK_EXIT exit "$YADM_HOOK_EXIT"
} }
@ -50,7 +57,7 @@ function ensure_command {
function ensure_algorithm { function ensure_algorithm {
# check if "shasum" supports algorithm # check if "shasum" supports algorithm by hashing an empty string
echo -n | shasum --algorithm "$CHECKSUM_ALGORITHM" &> /dev/null echo -n | shasum --algorithm "$CHECKSUM_ALGORITHM" &> /dev/null
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
@ -60,9 +67,9 @@ function ensure_algorithm {
# check whether file with checksums exists # check whether file with checksums exists
if [ ! -f $YADM_CHECKSUMS ]; then if [ ! -f "$YADM_CHECKSUMS" ]; then
# return exit status of the yadm command # return exit status of the yadm command
exit $YADM_HOOK_EXIT exit "$YADM_HOOK_EXIT"
fi fi
# check if "shasum" exists and supports algorithm # check if "shasum" exists and supports algorithm
@ -70,7 +77,7 @@ ensure_command shasum
ensure_algorithm ensure_algorithm
# check encrypted files for differences and capture output # check encrypted files for differences and capture output
YADM_CHECKSUM_OUTPUT=$(shasum --algorithm "$CHECKSUM_ALGORITHM" --check $YADM_CHECKSUMS 2> /dev/null) YADM_CHECKSUM_OUTPUT=$(shasum --algorithm "$CHECKSUM_ALGORITHM" --check "$YADM_CHECKSUMS" 2> /dev/null)
ERROR_CODE=$? ERROR_CODE=$?
# some checksums do not match # some checksums do not match
@ -85,9 +92,9 @@ if [ $ERROR_CODE -ne 0 ]; then
while IFS= read -r line; do while IFS= read -r line; do
# try to beautify output # try to beautify output
if command -v grep > /dev/null && command -v sed > /dev/null; then if command -v grep > /dev/null && command -v sed > /dev/null; then
echo $line | grep -iv "\sok$" | sed 's/^/ / ; s/: FAILED$//' echo "$line" | grep -iv "\sok$" | sed 's/^/ / ; s/: FAILED$//'
else else
echo $line echo "$line"
fi fi
done <<< "$YADM_CHECKSUM_OUTPUT" done <<< "$YADM_CHECKSUM_OUTPUT"

16
yadm
View File

@ -1577,7 +1577,11 @@ function invoke_hook() {
YADM_HOOK_FULL_COMMAND=$FULL_COMMAND YADM_HOOK_FULL_COMMAND=$FULL_COMMAND
YADM_HOOK_REPO=$YADM_REPO YADM_HOOK_REPO=$YADM_REPO
YADM_HOOK_WORK=$YADM_WORK YADM_HOOK_WORK=$YADM_WORK
YADM_ENCRYPT_INCLUDE_FILES=${ENCRYPT_INCLUDE_FILES[@]}
# pack array to export it; filenames including a newline character (\n)
# are NOT supported
YADM_ENCRYPT_INCLUDE_FILES=$(join_string $'\n' "${ENCRYPT_INCLUDE_FILES[@]}")
export YADM_HOOK_COMMAND export YADM_HOOK_COMMAND
export YADM_HOOK_EXIT export YADM_HOOK_EXIT
export YADM_HOOK_FULL_COMMAND export YADM_HOOK_FULL_COMMAND
@ -1821,6 +1825,13 @@ function auto_bootstrap() {
} }
# ****** Helper Functions ******
function join_string {
local IFS="$1"
echo "${*:2}"
}
# ****** Prerequisites Functions ****** # ****** Prerequisites Functions ******
function require_archive() { function require_archive() {
@ -1888,7 +1899,7 @@ function readlink_available() {
return 1 return 1
} }
# ****** Directory tranlations ****** # ****** Directory translations ******
function unix_path() { function unix_path() {
# for paths used by bash/yadm # for paths used by bash/yadm
@ -1908,6 +1919,7 @@ function mixed_path() {
} }
# ****** echo replacements ****** # ****** echo replacements ******
function echo() { function echo() {
IFS=' ' IFS=' '
printf '%s\n' "$*" printf '%s\n' "$*"