diff --git a/example.sh b/example.sh index eff4ff2..3c16277 100755 --- a/example.sh +++ b/example.sh @@ -43,7 +43,7 @@ read -r -d '' __helptext <<-'EOF' || true # exits non-zero when EOF encountered parsed and will be added as-is to the help. EOF -# shellcheck source=./main.sh +# shellcheck source=main.sh source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/main.sh" diff --git a/src/ini_val.sh b/src/ini_val.sh index 22f4bf7..86cf22a 100755 --- a/src/ini_val.sh +++ b/src/ini_val.sh @@ -35,13 +35,15 @@ function ini_val() { local key="" # Split on . for section. However, section is optional - read section key <<<$(IFS="."; echo ${sectionkey}) + IFS='.' read -r section key <<< "${sectionkey}" if [ -z "${key}" ]; then key="${section}" section="" fi - local current=$(awk -F "${delim}" "/^${key}${delim}/ {for (i=2; i /dev/null 2>&1 || true mkdir -p "${target}" diff --git a/src/parse_url.sh b/src/parse_url.sh index 09bac4b..5e004c7 100644 --- a/src/parse_url.sh +++ b/src/parse_url.sh @@ -30,15 +30,25 @@ function parse_url() { local parse="${1}" local need="${2:-}" - local proto="$(echo $parse | grep :// | sed -e's,^\(.*://\).*,\1,g')" - local url="$(echo ${parse/$proto/})" - local userpass="$(echo $url | grep @ | cut -d@ -f1)" - local user="$(echo $userpass | grep : | cut -d: -f1)" - local pass="$(echo $userpass | grep : | cut -d: -f2)" - local hostport="$(echo ${url/$userpass@/} | cut -d/ -f1)" - local host="$(echo $hostport | grep : | cut -d: -f1)" - local port="$(echo $hostport | grep : | cut -d: -f2)" - local path="$(echo $url | grep / | cut -d/ -f2-)" + local proto + local url + local userpass + local user + local pass + local hostport + local host + local port + local path + + proto="$(echo "$parse" | grep :// | sed -e's,^\(.*://\).*,\1,g')" + url="${parse/$proto/}" + userpass="$(echo "$url" | grep @ | cut -d@ -f1)" + user="$(echo "$userpass" | grep : | cut -d: -f1)" + pass="$(echo "$userpass" | grep : | cut -d: -f2)" + hostport="$(echo "${url/$userpass@/}" | cut -d/ -f1)" + host="$(echo "$hostport" | grep : | cut -d: -f1)" + port="$(echo "$hostport" | grep : | cut -d: -f2)" + path="$(echo "$url" | grep / | cut -d/ -f2-)" [ -z "${user}" ] && user="${userpass}" [ -z "${host}" ] && host="${hostport}" @@ -50,7 +60,7 @@ function parse_url() { fi if [ -n "${need}" ]; then - echo ${!need} + echo "${!need}" else echo "" echo " Use second argument to return just 1 variable." diff --git a/src/templater.sh b/src/templater.sh index 7046b23..344e9d2 100755 --- a/src/templater.sh +++ b/src/templater.sh @@ -48,6 +48,7 @@ function templater() { # cat "${templateDst}" + # shellcheck disable=SC2016 if grep '${' "${templateDst}" && [ "${ALLOW_REMAINDERS}" = "0" ]; then echo "ERROR: Unable to replace the above template vars" exit 1 diff --git a/test/acceptance.sh b/test/acceptance.sh index 47f22c0..e7694d6 100755 --- a/test/acceptance.sh +++ b/test/acceptance.sh @@ -201,11 +201,8 @@ if [[ "$(command -v shellcheck)" ]]; then failed="false" while IFS=$'\n' read -r file; do - lint="false" - [[ "${file}" = "./main.sh" ]] && lint="true" - [[ "${file}" = "./example.sh" ]] && lint="true" - [[ "${file}" = "./test/acceptance.sh" ]] && lint="true" - [[ "${lint}" != "true" ]] && continue + [[ "${file}" =~ ^\./node_modules/ ]] && continue + [[ "${file}" =~ ^\./website/\.lanyon/ ]] && continue echo -n " ${file}.. " diff --git a/test/scenario/ini_val/run.sh b/test/scenario/ini_val/run.sh index e042912..be5b2ae 100644 --- a/test/scenario/ini_val/run.sh +++ b/test/scenario/ini_val/run.sh @@ -7,8 +7,8 @@ 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)" +__base="$(basename "${__file}" .sh)" +__root="$(cd "$(dirname "$(dirname "$(dirname "${__dir}")")")" && pwd)" # echo "ACCPTST:STDIO_REPLACE_DATETIMES" @@ -29,7 +29,10 @@ rm -f dummy.ini # Use as include: cp -f data.ini dummy.ini -source ${__root}/src/ini_val.sh + +# shellcheck source=main.sh +source "${__root}/src/ini_val.sh" + echo "--> function: Read 3 values" ini_val ./dummy.ini orphan ini_val ./dummy.ini connection.host diff --git a/test/scenario/main-debug/run.sh b/test/scenario/main-debug/run.sh index 9517934..eda5bce 100644 --- a/test/scenario/main-debug/run.sh +++ b/test/scenario/main-debug/run.sh @@ -4,11 +4,8 @@ set -o errexit set -o nounset # set -o xtrace -# 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)" +__root="$(cd "$(dirname "$(dirname "$(dirname "${__dir}")")")" && pwd)" echo "ACCPTST:STDIO_REPLACE_DATETIMES" diff --git a/test/scenario/main-help/run.sh b/test/scenario/main-help/run.sh index f7b6ac7..b600c25 100755 --- a/test/scenario/main-help/run.sh +++ b/test/scenario/main-help/run.sh @@ -4,11 +4,8 @@ set -o errexit set -o nounset # set -o xtrace -# 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)" +__root="$(cd "$(dirname "$(dirname "$(dirname "${__dir}")")")" && pwd)" echo "ACCPTST:STDIO_REPLACE_DATETIMES" diff --git a/test/scenario/main-longopt/run.sh b/test/scenario/main-longopt/run.sh index 23ec6c5..3d2cc77 100644 --- a/test/scenario/main-longopt/run.sh +++ b/test/scenario/main-longopt/run.sh @@ -4,14 +4,13 @@ set -o nounset # set -o xtrace -# 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)" +__root="$(cd "$(dirname "$(dirname "$(dirname "${__dir}")")")" && pwd)" echo "ACCPTST:STDIO_REPLACE_DATETIMES" -(env LOG_LEVEL=6 bash "${__root}/main.sh" --file /tmp/x; -env LOG_LEVEL=6 bash "${__root}/main.sh" --file=/tmp/x; -env LOG_LEVEL=6 bash "${__root}/main.sh" -f /tmp/x) 2>&1 |grep arg_f +( + env LOG_LEVEL=6 bash "${__root}/main.sh" --file /tmp/x; + env LOG_LEVEL=6 bash "${__root}/main.sh" --file=/tmp/x; + env LOG_LEVEL=6 bash "${__root}/main.sh" -f /tmp/x +) 2>&1 |grep arg_f diff --git a/test/scenario/main-nocolor/run.sh b/test/scenario/main-nocolor/run.sh index 07e8012..8abf1cd 100644 --- a/test/scenario/main-nocolor/run.sh +++ b/test/scenario/main-nocolor/run.sh @@ -4,11 +4,8 @@ set -o errexit set -o nounset # set -o xtrace -# 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)" +__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 cd8d1f8..018084d 100755 --- a/test/scenario/main-usage-defaults/run.sh +++ b/test/scenario/main-usage-defaults/run.sh @@ -4,13 +4,9 @@ set -o errexit set -o nounset # set -o xtrace -# 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)" +__root="$(cd "$(dirname "$(dirname "$(dirname "${__dir}")")")" && pwd)" -# Set __usage and source main.sh read -r -d '' __usage <<-'EOF' || true # exits non-zero when EOF encountered -1 --one Do one thing. Default="ONE" More description. @@ -28,6 +24,7 @@ EOF echo "ACCPTST:STDIO_REPLACE_DATETIMES" +# shellcheck source=main.sh source "${__root}/main.sh" for argument in ${!arg_*}; do info "${argument}: ${!argument}"; done diff --git a/test/scenario/main-usage-validation/run.sh b/test/scenario/main-usage-validation/run.sh index e0cc98f..7525c03 100755 --- a/test/scenario/main-usage-validation/run.sh +++ b/test/scenario/main-usage-validation/run.sh @@ -1,16 +1,14 @@ #!/usr/bin/env bash +# shellcheck source=main.sh + set -o pipefail set -o errexit set -o nounset # set -o xtrace -# 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)" +__root="$(cd "$(dirname "$(dirname "$(dirname "${__dir}")")")" && pwd)" -# Set __usage and source main.sh read -r -d '' __usage <<-'EOF' || true # exits non-zero when EOF encountered -0 --zero Do nothing. -1 --one Do one thing. Required. diff --git a/test/scenario/megamount/run.sh b/test/scenario/megamount/run.sh index 5223965..517cff5 100644 --- a/test/scenario/megamount/run.sh +++ b/test/scenario/megamount/run.sh @@ -4,11 +4,8 @@ set -o errexit set -o nounset # set -o xtrace -# 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)" +__root="$(cd "$(dirname "$(dirname "$(dirname "${__dir}")")")" && pwd)" __sysTmpDir="${TMPDIR:-/tmp}" __sysTmpDir="${__sysTmpDir%/}" # <-- remove trailing slash on macosx @@ -18,5 +15,7 @@ __sysTmpDir="${__sysTmpDir%/}" # <-- remove trailing slash on macosx bash "${__root}/src/megamount.sh" 'foobarfs://janedoe:abc123@192.168.0.1/documents' "${__sysTmpDir}/mnt/documents" || true -source ${__root}/src/megamount.sh +# shellcheck source=src/megamount.sh +source "${__root}/src/megamount.sh" + megamount 'foobarfs://janedoe:abc123@192.168.0.1/documents' "${__sysTmpDir}/mnt/documents" diff --git a/test/scenario/parse_url/run.sh b/test/scenario/parse_url/run.sh index dd11b19..47a18aa 100644 --- a/test/scenario/parse_url/run.sh +++ b/test/scenario/parse_url/run.sh @@ -4,15 +4,14 @@ set -o errexit set -o nounset # set -o xtrace -# 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)" +__root="$(cd "$(dirname "$(dirname "$(dirname "${__dir}")")")" && pwd)" bash "${__root}/src/parse_url.sh" 'http://johndoe:abc123@example.com:8080/index.html' pass bash "${__root}/src/parse_url.sh" 'http://johndoe:abc123@example.com:8080/index.html' -source ${__root}/src/parse_url.sh +# shellcheck source=src/parse_url.sh +source "${__root}/src/parse_url.sh" + parse_url 'http://johndoe:abc123@example.com:8080/index.html' pass parse_url 'http://johndoe:abc123@example.com:8080/index.html' diff --git a/test/scenario/templater/run.sh b/test/scenario/templater/run.sh index 129fcef..6355e17 100644 --- a/test/scenario/templater/run.sh +++ b/test/scenario/templater/run.sh @@ -4,11 +4,8 @@ set -o errexit set -o nounset # set -o xtrace -# 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)" +__root="$(cd "$(dirname "$(dirname "$(dirname "${__dir}")")")" && pwd)" echo "--" env TARGET_HOST="127.0.0.1" bash "${__root}/src/templater.sh" ./app.template.cfg ./app.cfg @@ -17,7 +14,10 @@ rm -f app.cfg echo "--" export TARGET_HOST="127.0.0.1" -source ${__root}/src/templater.sh + +# shellcheck source=src/templater.sh +source "${__root}/src/templater.sh" + templater ./app.template.cfg ./app.cfg cat app.cfg rm -f app.cfg diff --git a/website/_scripts/inject.sh b/website/_scripts/inject.sh index 4dfe1db..5711e7a 100755 --- a/website/_scripts/inject.sh +++ b/website/_scripts/inject.sh @@ -30,7 +30,7 @@ set -o pipefail # Set magic variables for current file, directory, os, etc. __dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" __file="${__dir}/$(basename "${BASH_SOURCE[0]}")" -__base="$(basename ${__file} .sh)" +__base="$(basename "${__file}" .sh)" # Offer the main template for download as http://bash3boilerplate.sh/main.sh cp -v main.sh website/ @@ -38,10 +38,10 @@ cp -v main.sh website/ for doc in "README" "FAQ" "CHANGELOG"; do targetName="$(echo "${doc}" | awk '{print tolower($0)}')" permalink="/${targetName}/" - subtitle="$(tr '[:lower:]' '[:upper:]' <<< ${targetName:0:1})${targetName:1} | " + subtitle="$(tr '[:lower:]' '[:upper:]' <<< "${targetName:0:1}")${targetName:1} | " redirectFrom="/${doc}.md/" backLink="\n\n« Home" - if [ "${doc}" = "README" ]; then + if [[ "${doc}" = "README" ]]; then targetName="index" permalink="/" subtitle="" @@ -49,7 +49,7 @@ for doc in "README" "FAQ" "CHANGELOG"; do backLink="" fi - cat < website/${targetName}.md + cat < "website/${targetName}.md" --- layout: default permalink: ${permalink} @@ -60,14 +60,14 @@ warning: This page is generated by ${__base}.sh based on ${doc}.md, please don't EOF # If '' exists, only inject what comes after it, so you can have e.g. a ToC or buildbuttons # on GitHub, without that also rendering in the site (site may have its own ToC rendering for instance) - if grep '' ${doc}.md; then - cat ${doc}.md |sed -n -e '//,$p' | tail -n +2 >> website/${targetName}.md + if grep '' "${doc}.md"; then + sed -n -e '//,$p' "${doc}.md" | tail -n +2 >> "website/${targetName}.md" else - cat ${doc}.md >> website/${targetName}.md + cat "${doc}.md" >> "website/${targetName}.md" fi # Add a "<- Back Home" link, if any - echo -e $backLink >> website/${targetName}.md + echo -e $backLink >> "website/${targetName}.md" echo "--> written website/${targetName}.md" done