Fix most of the errors and warnings reported by shellcheck

This commit is contained in:
Éfrit 2020-04-01 02:40:55 +02:00
parent 73aafa56e1
commit f4765bfa54
8 changed files with 26 additions and 20 deletions

View File

@ -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"
})"

View File

@ -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"

View File

@ -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"
}

View File

@ -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."
)"

View File

@ -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

View File

@ -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

View File

@ -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"

View File

@ -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"