From 236e3c7eca80e9838e4c14a3cfc3a0f9616772a3 Mon Sep 17 00:00:00 2001 From: Manuel Streuhofer Date: Sun, 21 Jan 2018 23:02:03 +0100 Subject: [PATCH] fix shellcheck errors shellcheck 0.4.7 seems to have come up with new checks and now complains about yet more possible issues. fixed them all as good as i could. --- example.sh | 9 +++- main.sh | 24 ++++----- src/ini_val.sh | 2 +- test/acceptance.sh | 62 ++++++++++++------------ test/scenario/ini_val/run.sh | 2 - test/scenario/main-usage-defaults/run.sh | 2 + website/_scripts/inject.sh | 2 +- 7 files changed, 54 insertions(+), 49 deletions(-) diff --git a/example.sh b/example.sh index c1c69e5..1ab616e 100755 --- a/example.sh +++ b/example.sh @@ -27,6 +27,7 @@ # - We do not bash-expand defaults, so setting '~/app' as a default will not resolve to ${HOME}. # you can use bash variables to work around this (so use ${HOME} instead) +# shellcheck disable=SC2034 read -r -d '' __usage <<-'EOF' || true # exits non-zero when EOF encountered -f --file [arg] Filename to process. Required. -t --temp [arg] Location of tempfile. Default="/tmp/bar" @@ -37,6 +38,7 @@ read -r -d '' __usage <<-'EOF' || true # exits non-zero when EOF encountered -1 --one Do just one thing EOF +# shellcheck disable=SC2034 read -r -d '' __helptext <<-'EOF' || true # exits non-zero when EOF encountered This is Bash3 Boilerplate's help text. Feel free to add any description of your program or elaborate more on command-line arguments. This section is not @@ -59,6 +61,7 @@ trap __b3bp_cleanup_before_exit EXIT __b3bp_err_report() { local error_code error_code=${?} + # shellcheck disable=SC2154 error "Error in ${__file} in function ${1} on line ${2}" exit ${error_code} } @@ -104,9 +107,13 @@ fi ### Runtime ############################################################################## +# shellcheck disable=SC2154 info "__i_am_main_script: ${__i_am_main_script}" +# shellcheck disable=SC2154 info "__file: ${__file}" +# shellcheck disable=SC2154 info "__dir: ${__dir}" +# shellcheck disable=SC2154 info "__base: ${__base}" info "OSTYPE: ${OSTYPE}" @@ -115,7 +122,7 @@ info "arg_d: ${arg_d}" info "arg_v: ${arg_v}" info "arg_h: ${arg_h}" -info "$(echo -e "multiple lines example - line #1\nmultiple lines example - line #2\nimagine logging the output of 'ls -al /path/'")" +info "$(echo -e "multiple lines example - line #1\\nmultiple lines example - line #2\\nimagine logging the output of 'ls -al /path/'")" # All of these go to STDERR, so you can use STDOUT for piping machine readable information to other software debug "Info useful to developers for debugging the application, not useful during operations." diff --git a/main.sh b/main.sh index fec3475..0ed7447 100755 --- a/main.sh +++ b/main.sh @@ -62,26 +62,26 @@ function __b3bp_log () { shift # shellcheck disable=SC2034 - local color_debug="\x1b[35m" + local color_debug="\\x1b[35m" # shellcheck disable=SC2034 - local color_info="\x1b[32m" + local color_info="\\x1b[32m" # shellcheck disable=SC2034 - local color_notice="\x1b[34m" + local color_notice="\\x1b[34m" # shellcheck disable=SC2034 - local color_warning="\x1b[33m" + local color_warning="\\x1b[33m" # shellcheck disable=SC2034 - local color_error="\x1b[31m" + local color_error="\\x1b[31m" # shellcheck disable=SC2034 - local color_critical="\x1b[1;31m" + local color_critical="\\x1b[1;31m" # shellcheck disable=SC2034 - local color_alert="\x1b[1;33;41m" + local color_alert="\\x1b[1;33;41m" # shellcheck disable=SC2034 - local color_emergency="\x1b[1;4;5;33;41m" + local color_emergency="\\x1b[1;4;5;33;41m" local colorvar="color_${log_level}" local color="${!colorvar:-${color_error}}" - local color_reset="\x1b[0m" + local color_reset="\\x1b[0m" if [[ "${NO_COLOR:-}" = "true" ]] || ( [[ "${TERM:-}" != "xterm"* ]] && [[ "${TERM:-}" != "screen"* ]] ) || [[ ! -t 2 ]]; then if [[ "${NO_COLOR:-}" != "false" ]]; then @@ -190,7 +190,7 @@ while read -r __b3bp_tmp_line; do [[ "${__b3bp_tmp_opt:-}" ]] || continue - if [[ "${__b3bp_tmp_line}" =~ (^|\.\ *)Default= ]]; then + if [[ "${__b3bp_tmp_line}" =~ ^Default= ]] || [[ "${__b3bp_tmp_line}" =~ \.\ *Default= ]]; then # ignore default value if option does not have an argument __b3bp_tmp_varname="__b3bp_tmp_has_arg_${__b3bp_tmp_opt:0:1}" @@ -208,7 +208,7 @@ while read -r __b3bp_tmp_line; do fi fi - if [[ "${__b3bp_tmp_line}" =~ (^|\.\ *)Required\. ]]; then + if [[ "${__b3bp_tmp_line}" =~ ^Required\. ]] || [[ "${__b3bp_tmp_line}" =~ \.\ *Required\. ]]; then # remember that this option requires an argument printf -v "__b3bp_tmp_has_arg_${__b3bp_tmp_opt:0:1}" '%s' "2" fi @@ -380,7 +380,7 @@ info "arg_d: ${arg_d}" info "arg_v: ${arg_v}" info "arg_h: ${arg_h}" -info "$(echo -e "multiple lines example - line #1\nmultiple lines example - line #2\nimagine logging the output of 'ls -al /path/'")" +info "$(echo -e "multiple lines example - line #1\\nmultiple lines example - line #2\\nimagine logging the output of 'ls -al /path/'")" # All of these go to STDERR, so you can use STDOUT for piping machine readable information to other software debug "Info useful to developers for debugging the application, not useful during operations." diff --git a/src/ini_val.sh b/src/ini_val.sh index 15bbd57..9791f5d 100755 --- a/src/ini_val.sh +++ b/src/ini_val.sh @@ -57,7 +57,7 @@ function ini_val() { echo "${key}${delim}${val}" >> "${file}" else # add to section - sed -i.bak -e "/\[${section}\]/a ${key}${delim}${val}" "${file}" + sed -i.bak -e "/\\[${section}\\]/a ${key}${delim}${val}" "${file}" # this .bak dance is done for BSD/GNU portability: http://stackoverflow.com/a/22084103/151666 rm -f "${file}.bak" fi diff --git a/test/acceptance.sh b/test/acceptance.sh index 35c0a73..0f0d956 100755 --- a/test/acceptance.sh +++ b/test/acceptance.sh @@ -41,8 +41,8 @@ __accptstTmpDir=$(mktemp -d "${__sysTmpDir}/${__base}.XXXXXX") function cleanup_before_exit () { rm -r "${__accptstTmpDir:?}"; } trap cleanup_before_exit EXIT -cmdSed=sed -cmdTimeout=timeout +cmdSed="sed" +cmdTimeout="timeout" if [[ "${OSTYPE}" = "darwin"* ]]; then cmdSed=gsed @@ -98,7 +98,7 @@ while IFS=$'\n' read -r scenario; do -e "s@${USER:-travis}@{user}@g" "${curFile}" \ -e "s@travis@{user}@g" "${curFile}" \ -e "s@kvz@{user}@g" "${curFile}" \ - -e "s@{root}/node_modules/\.bin/node@{node}@g" "${curFile}" \ + -e "s@{root}/node_modules/\\.bin/node@{node}@g" "${curFile}" \ -e "s@{home}/build/{user}/fre{node}@{node}@g" "${curFile}" \ -e "s@${HOSTNAME}@{hostname}@g" "${curFile}" \ -e "s@${__arch}@{arch}@g" "${curFile}" \ @@ -177,10 +177,10 @@ while IFS=$'\n' read -r scenario; do fi if ! diff --strip-trailing-cr "${__dir}/fixture/${scenario}.${typ}" "${curFile}"; then - echo -e "\n\n==> MISMATCH OF: ${scenario}.${typ} ---^" - echo -e "\n\n==> EXPECTED STDIO: " + echo -e "\\n\\n==> MISMATCH OF: ${scenario}.${typ} ---^" + echo -e "\\n\\n==> EXPECTED STDIO: " cat "${__dir}/fixture/${scenario}.stdio" || true - echo -e "\n\n==> ACTUAL STDIO: " + echo -e "\\n\\n==> ACTUAL STDIO: " cat "${__accptstTmpDir}/${scenario}.stdio" || true exit 1 fi @@ -195,37 +195,35 @@ done <<< "$(find "${__dir}/scenario" -type f -iname 'run.sh')" # Ensure correct syntax with all available bashes -if bashes=($(which -a bash 2> /dev/null )); then - for bash in "${bashes[@]}"; do - # shellcheck disable=SC2016 - echo "==> ${bash} -n $(${bash} -c 'echo "(${BASH_VERSION})"')" - pushd "${__root}" > /dev/null +while IFS=$'\n' read -r bash; do + # shellcheck disable=SC2016 + echo "==> ${bash} -n $(${bash} -c 'echo "(${BASH_VERSION})"')" + pushd "${__root}" > /dev/null - failed="false" + failed="false" - while IFS=$'\n' read -r file; do - [[ "${file}" =~ ^\./node_modules/ ]] && continue - [[ "${file}" =~ ^\./website/\.lanyon/ ]] && continue + while IFS=$'\n' read -r file; do + [[ "${file}" =~ ^\./node_modules/ ]] && continue + [[ "${file}" =~ ^\./website/\.lanyon/ ]] && continue - echo -n " ${file}.. " + echo -n " ${file}.. " - if ! "${bash}" -n "${file}" 2>> "${__accptstTmpDir}/${bash//\//.}.err"; then - echo "✗" - failed="true" - continue - fi - - echo "✓" - done <<< "$(find . -type f -iname '*.sh')" - - popd > /dev/null - - if [[ "${failed}" = "true" ]]; then - cat "${__accptstTmpDir}/${bash//\//.}.err" - exit 1 + if ! "${bash}" -n "${file}" 2>> "${__accptstTmpDir}/${bash//\//.}.err"; then + echo "✗" + failed="true" + continue fi - done -fi + + echo "✓" + done <<< "$(find . -type f -iname '*.sh')" + + popd > /dev/null + + if [[ "${failed}" = "true" ]]; then + cat "${__accptstTmpDir}/${bash//\//.}.err" + exit 1 + fi +done <<< "$(which -a bash 2>/dev/null)" # do some shellcheck linting if [[ "$(command -v shellcheck)" ]]; then diff --git a/test/scenario/ini_val/run.sh b/test/scenario/ini_val/run.sh index be5b2ae..8b98c1c 100644 --- a/test/scenario/ini_val/run.sh +++ b/test/scenario/ini_val/run.sh @@ -6,8 +6,6 @@ set -o nounset # Set magic variables for current FILE & DIR __dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -__file="${__dir}/$(basename "${BASH_SOURCE[0]}")" -__base="$(basename "${__file}" .sh)" __root="$(cd "$(dirname "$(dirname "$(dirname "${__dir}")")")" && pwd)" # echo "ACCPTST:STDIO_REPLACE_DATETIMES" diff --git a/test/scenario/main-usage-defaults/run.sh b/test/scenario/main-usage-defaults/run.sh index 018084d..d0c7703 100755 --- a/test/scenario/main-usage-defaults/run.sh +++ b/test/scenario/main-usage-defaults/run.sh @@ -22,6 +22,8 @@ read -r -d '' __usage <<-'EOF' || true # exits non-zero when EOF encountered More description. EOF +export __usage + echo "ACCPTST:STDIO_REPLACE_DATETIMES" # shellcheck source=main.sh diff --git a/website/_scripts/inject.sh b/website/_scripts/inject.sh index 6566178..1aca08b 100755 --- a/website/_scripts/inject.sh +++ b/website/_scripts/inject.sh @@ -42,7 +42,7 @@ for doc in "README" "FAQ" "CHANGELOG"; do permalink="/${targetName}/" subtitle="$(tr '[:lower:]' '[:upper:]' <<< "${targetName:0:1}")${targetName:1} | " redirectFrom="/${doc}.md/" - backLink="\n\n« Home" + backLink='\n\n« Home' if [[ "${doc}" = "README" ]]; then targetName="index" permalink="/"