From 1f47fc0a18a062111d2b17717c51baea460b4edf Mon Sep 17 00:00:00 2001 From: Jonathan Bauer Date: Tue, 26 May 2020 12:46:28 +0200 Subject: build-initramfs.sh: shellcheck --- build-initramfs.sh | 66 ++++++++++++++++++++++++++---------------------------- 1 file changed, 32 insertions(+), 34 deletions(-) (limited to 'build-initramfs.sh') diff --git a/build-initramfs.sh b/build-initramfs.sh index 303e3186..4c280661 100755 --- a/build-initramfs.sh +++ b/build-initramfs.sh @@ -19,7 +19,7 @@ # |- build-initramfs.sh # |- dracut # |- systemd-init -declare -rg _root_dir="$(readlink -f $(dirname ${BASH_SOURCE[0]}))" +declare -rg _root_dir="$(readlink -f "$(dirname "${BASH_SOURCE[0]}")")" # TODO clean these up file_path='/boot/initramfs.img' @@ -97,17 +97,16 @@ declare -A override bootstrap() { for module in "${!core_@}" "${!module_@}"; do - declare -n _ref="$module" - if [ "$?" -ne 0 ]; then + if declare -n _ref="$module"; then echo "Only bash >= 4.3 supports namerefs." \ "You are running ${BASH_VERSION}." \ "Falling back to using evil eval..." eval 'declare -A _ref=(' \ - '[handler]=${'$module'[handler]}' \ - '[path]=${'$module'[path]}' \ - '[url]=${'$module'[url]}' \ - '[branch]=${'$module'[branch]}' \ - '[commit]=${'$module'[commit]}' \ + '[handler]=${'"$module"'[handler]}' \ + '[path]=${'"$module"'[path]}' \ + '[url]=${'"$module"'[url]}' \ + '[branch]=${'"$module"'[branch]}' \ + '[commit]=${'"$module"'[commit]}' \ ')' # phew that was ugly.... fi @@ -132,7 +131,7 @@ bootstrap() { echo " Path: ${_ref[path]}" # handlers deal with errors on their own - handler_${_ref[handler]} \ + handler_"${_ref[handler]}" \ "${_ref[path]}" \ "${_ref[url]}" \ "${_ref[branch]}" \ @@ -140,13 +139,13 @@ bootstrap() { # apply patches if any are required shopt -s nullglob - pushd "${_ref[path]}" + pushd "${_ref[path]}" || exit 1 for patch in "${_repo_dir}/patches/${_ref[path]##*/}/"*.patch; do if ! patch -p1 < "$patch"; then echo "Applying '$patch' failed, expecting errors. Continuing..." fi done - popd + popd || exit 1 done } @@ -180,10 +179,10 @@ handler_git() { fi # manual "shallow clone" since not all server allow it... - pushd "$path" + pushd "$path" || exit 1 local i=0 local last_count=0 - while ! git rev-parse --quiet --verify $revision^{commit}; do + while ! git rev-parse --quiet --verify "${revision}^{commit}"; do current_count="$(git rev-list --count --all)" if [ "$current_count" -eq "$last_count" ]; then echo "Failed to find '$revision' in '$2'." @@ -193,7 +192,7 @@ handler_git() { git fetch --depth=$(( i+=50 )) done git reset --hard "$revision" - popd + popd || exit 1 } handler_http() { @@ -207,20 +206,19 @@ handler_http() { mkdir --parents "$path" set -o pipefail - curl \ - --location \ - --max-redirs 5 \ - --max-time 7 \ - --connect-timeout 2 \ - --retry 3 \ - --retry-max-time 12 \ - "$url" \ - | tar \ - --extract \ - --gzip \ - --directory "$path" \ - --strip-components 1 - if [ $? -ne 0 ]; then + if ! curl \ + --location \ + --max-redirs 5 \ + --max-time 7 \ + --connect-timeout 2 \ + --retry 3 \ + --retry-max-time 12 \ + "$url" \ + | tar \ + --extract \ + --gzip \ + --directory "$path" \ + --strip-components 1; then echo "Failed to download/extract '$url' to '$path'." exit 1 fi @@ -368,7 +366,7 @@ parse_command_line() { ## endregion ## region helper initialize_dracut() { - pushd "${_dracut_dir}" + pushd "${_dracut_dir}" || exit 1 # NOTE: On virtualbox shared folder symlinks are not allowed. # NOTE: make the dracut-install binary (dracut-install resolves # dependencies etc.) @@ -380,7 +378,7 @@ initialize_dracut() { # folders. # If symlinks would be available we could simply use: # >>> make dracut-install - popd + popd || exit 1 cp "${_dracut_dir}/install/dracut-install" \ "${_dracut_dir}/dracut-install" return $? @@ -440,9 +438,9 @@ main() { fi if [ "$update" = "yes" ]; then - pushd "${_repo_dir}" + pushd "${_repo_dir}" || exit 1 git pull - popd + popd || exit 1 fi echo 'Checking dracut...' @@ -453,7 +451,7 @@ main() { for _dracut_module_dir in "${_repo_dir}/modules.d/"*; do [ -d "${_dracut_module_dir}" ] || continue - _dracut_module="$(basename $_dracut_module_dir)" + _dracut_module="$(basename "$_dracut_module_dir")" # TODO allow module-specific priority _dracut_module_target="${_dracut_dir}/modules.d/00${_dracut_module}" if [[ ! -L "$_dracut_module_target" || "$(readlink \ @@ -477,7 +475,7 @@ main() { # default dracut modules _modules=(dnbd3-rootfs conf-tgz) - echo "Default modules: ${_modules[@]}" + echo "Default modules: ${_modules[*]}" if [ "$verbose" = "yes" ]; then dracut_parameters+=("--verbose") fi -- cgit v1.2.3-55-g7522