From f4765bfa54aa2da36ef010096e998c90da8a21aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89frit?= <8625408+VeryTastyTomato@users.noreply.github.com> Date: Wed, 1 Apr 2020 02:40:55 +0200 Subject: [PATCH] Fix most of the errors and warnings reported by shellcheck --- build.sh | 10 ++++++---- lib/pager.sh | 3 ++- lib/str.sh | 4 ++-- src/batgrep.sh | 2 +- src/batman.sh | 10 ++++++---- src/batwatch.sh | 4 ++-- src/prettybat.sh | 11 ++++++----- test.sh | 2 +- 8 files changed, 26 insertions(+), 20 deletions(-) diff --git a/build.sh b/build.sh index 1d7eafe..fc42bb6 100755 --- a/build.sh +++ b/build.sh @@ -75,7 +75,8 @@ step_preprocess() { # Replace the DOCS_* variables. if [[ "$line" =~ ^DOCS_[A-Z]+=.*$ ]]; then - local docvar="$(cut -d'=' -f1 <<<"$line")" + local docvar + docvar="$(cut -d'=' -f1 <<<"$line")" printf "%s=%q\n" "$docvar" "${!docvar}" continue fi @@ -83,13 +84,13 @@ step_preprocess() { # Embed library scripts. if [[ "$line" =~ ^[[:space:]]*source[[:space:]]+[\"\']\$\{?LIB\}/([a-z_-]+\.sh)[\"\'] ]]; then echo "# --- BEGIN LIBRARY FILE: ${BASH_REMATCH[1]} ---" - cat "$LIB/${BASH_REMATCH[1]}" | { + { if [[ "$OPT_MINIFY" = "lib" ]]; then pp_strip_comments | pp_minify | pp_minify_unsafe else cat fi - } + } <"$LIB/${BASH_REMATCH[1]}" echo "# --- END LIBRARY FILE ---" continue fi @@ -136,7 +137,8 @@ step_compress() { return 0 fi - local wrapper="$({ + local wrapper + wrapper="$({ printf '#!/usr/bin/env bash\n' printf "(exec -a \"\$0\" bash -c 'eval \"\$(cat <&3)\"' \"\$0\" \"\$@\" 3< <(dd bs=1 if=\"\$0\" skip=::: 2>/dev/null | gunzip)); exit \$?;\n" })" diff --git a/lib/pager.sh b/lib/pager.sh index b8caf82..6e4f2e6 100644 --- a/lib/pager.sh +++ b/lib/pager.sh @@ -22,7 +22,8 @@ is_pager_disabled() { pager_name() { if [[ -z "${SCRIPT_PAGER_CMD[0]}" ]]; then return; fi if [[ -z "$_SCRIPT_PAGER_NAME" ]]; then - local output="$("${SCRIPT_PAGER_CMD[0]}" --version 2>&1)" + local output + output="$("${SCRIPT_PAGER_CMD[0]}" --version 2>&1)" if head -n 1 <<<"$output" | grep '^less \d' &>/dev/null; then _SCRIPT_PAGER_NAME="less" diff --git a/lib/str.sh b/lib/str.sh index fb6b9d1..9e526c8 100644 --- a/lib/str.sh +++ b/lib/str.sh @@ -8,10 +8,10 @@ # Converts a string to lower case. tolower() { - tr "[[:upper:]]" "[[:lower:]]" <<<"$1" + tr "[:upper:]" "[:lower:]" <<<"$1" } # Converts a string to upper case. toupper() { - tr "[[:lower:]]" "[[:upper:]]" <<<"$1" + tr "[:lower:]" "[:upper:]" <<<"$1" } diff --git a/src/batgrep.sh b/src/batgrep.sh index 7788d6c..238f4f7 100755 --- a/src/batgrep.sh +++ b/src/batgrep.sh @@ -169,7 +169,7 @@ if "$OPT_SEARCH_PATTERN"; then fi elif is_pager_disabled; then print_error "$( - echo "The -p/--search-pattern option requires a pager, but" \ + "The -p/--search-pattern option requires a pager, but" \ "the pager was explicitly disabled by \$BAT_PAGER or the" \ "--paging option." )" diff --git a/src/batman.sh b/src/batman.sh index 6bcf042..488bc15 100755 --- a/src/batman.sh +++ b/src/batman.sh @@ -21,16 +21,18 @@ BAT_ARGS=() while shiftopt; do MAN_ARGS+=("$OPT"); done if "$OPT_COLOR"; then - BAT_ARGS="--color=always --decorations=always" + BAT_ARGS=("--color=always --decorations=always") else - BAT_ARGS="--color=never --decorations=never" + BAT_ARGS=("--color=never --decorations=never") fi # ----------------------------------------------------------------------------- -export MANPAGER='sh -c "col -bx | '"$(printf "%q" "$BAT")"' --language=man --style=grid '"${BAT_ARGS[@]}"'"' +export MANPAGER +MANPAGER='sh -c "col -bx | '"$(printf "%q" "$BAT")"' --language=man --style=grid '"${BAT_ARGS[*]}"'"' export MANROFFOPT='-c' if [[ -n "${SCRIPT_PAGER_CMD}" ]]; then - export BAT_PAGER="$(printf "%q " "${SCRIPT_PAGER_CMD[@]}" "${SCRIPT_PAGER_ARGS[@]}")" + export BAT_PAGER + BAT_PAGER="$(printf "%q " "${SCRIPT_PAGER_CMD[@]}" "${SCRIPT_PAGER_ARGS[@]}")" else unset BAT_PAGER fi diff --git a/src/batwatch.sh b/src/batwatch.sh index c8c262c..fc6e275 100755 --- a/src/batwatch.sh +++ b/src/batwatch.sh @@ -184,7 +184,7 @@ while shiftopt; do esac done -if [[ -z "$FILES" ]]; then +if [[ ${#FILES[@]} -eq 0 ]]; then print_error "no files provided" exit 1 fi @@ -213,7 +213,7 @@ fi # Determine the watcher. if [[ -z "$OPT_WATCHER" ]]; then OPT_WATCHER="$(determine_watcher)" - if [[ $? -ne 0 ]]; then + if ! "$OPT_WATCHER"; then print_error "Your system does not have any supported watchers." printc "Please read the documentation at %{BLUE}%s%{CLEAR} for more details.\n" "$DOCS_URL/batwatch.md" 1>&2 exit 2 diff --git a/src/prettybat.sh b/src/prettybat.sh index 3e0e19f..ffe24cb 100755 --- a/src/prettybat.sh +++ b/src/prettybat.sh @@ -152,7 +152,8 @@ process_file() { fext="$(map_language_to_extension "$lang")" fi - local formatter="$(map_extension_to_formatter "$fext")" + local formatter + formatter="$(map_extension_to_formatter "$fext")" echo "FORMATTER >>> $formatter" if [[ "$formatter" = "none" ]]; then if [[ -z "$OPT_LANGUAGE" ]]; then @@ -170,14 +171,14 @@ process_file() { if [[ "$file" = "-" ]]; then data_raw="$(cat -)" data_formatted="$("formatter_${formatter}_process" "$file" 2>/dev/null <<<"$data_raw")" - if [[ $? -ne 0 ]]; then + if "$data_formatted"; then print_warning "'STDIN': Unable to format with '%s'" "$formatter" print_file --language="$lang" - <<<"$data_raw" return 1 fi else data_formatted="$("formatter_${formatter}_process" "$file" <"$file")" - if [[ $? -ne 0 ]]; then + if ! "$data_formatted"; then print_warning "'%s': Unable to format with '%s'" "$file" "$formatter" print_file --language="$lang" "$file" return 1 @@ -224,7 +225,7 @@ while shiftopt; do done if [[ "${#FILES[@]}" -eq 0 ]]; then - FILES="-" + FILES=("-") fi # Handle input files. @@ -236,4 +237,4 @@ for file in "${FILES[@]}"; do done # Exit. -exit $EXIT +exit "$EXIT" diff --git a/test.sh b/test.sh index ca46713..ce66b8c 100755 --- a/test.sh +++ b/test.sh @@ -9,7 +9,7 @@ HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" LIB="${HERE}/lib" source "${LIB}/opt.sh" # ----------------------------------------------------------------------------- -cd "$HERE" +cd "$HERE" || exit # ----------------------------------------------------------------------------- export TEST_ENV_LIB="${HERE}/lib"