From 3548b97d7e0c4f53592dd850dfcfd35908e6c179 Mon Sep 17 00:00:00 2001 From: Oliver Sanders Date: Thu, 4 Apr 2019 12:59:41 +0100 Subject: [PATCH] shellcheck: bin/ --- bin/rose | 49 ++++++++++++++++++----------------------- bin/rose-edit | 2 +- bin/rose-make-docs | 41 +++++++++++++++++----------------- bin/rose-mpi-launch | 32 +++++++++++++++------------ bin/rose-slv | 2 +- bin/rose-suite-init | 2 +- bin/rose-suite-log-view | 2 +- bin/rose-suite-stop | 2 +- bin/rose-task-hook | 2 +- bin/rose-test-battery | 9 +++++--- bin/rose-tutorial | 9 ++++---- bin/rosie-co | 2 +- bin/rosie-copy | 2 +- 13 files changed, 78 insertions(+), 78 deletions(-) diff --git a/bin/rose b/bin/rose index bd1a65333f..2598f8aef0 100755 --- a/bin/rose +++ b/bin/rose @@ -37,20 +37,14 @@ if ${ROSE_DEBUG:-false}; then set -x fi -. $(dirname $0)/../lib/bash/rose_init +. "$(dirname "$0")/../lib/bash/rose_init" rose_init # Print actual command of a command alias get_alias() { - local NAME=$1 - local ALIAS=$(sed '/^#/d' $ROSE_HOME_BIN/$ROSE_NS-$NAME || true) - if [[ $(wc -l <<<"$ALIAS") == 1 ]] \ - && grep -q "^exec \$(dirname \$0)/$ROSE_NS-.* \"\$@\"\$" <<<"$ALIAS" - then - ALIAS=${ALIAS#"exec \$(dirname \$0)/$ROSE_NS-"} - ALIAS=${ALIAS%' "$@"'} - echo $ALIAS - fi + local NAME="$1" + sed -n 's/exec.*'"$ROSE_NS"'-\([^"]*\).*/\1/p' \ + "$ROSE_HOME_BIN/$ROSE_NS-$NAME" } # Print help for a given utility @@ -61,18 +55,19 @@ help_util() { echo "$1: utility not found." >&2 return 1 fi - local ALIAS=$(get_alias $NAME) + local ALIAS + ALIAS="$(get_alias "$NAME")" if [[ -n $ALIAS ]]; then COMMAND=$ROSE_HOME_BIN/$ROSE_NS-$ALIAS COMMAND=${COMMAND%% *} fi - case $(head -1 -- $COMMAND) in + case $(head -1 -- "$COMMAND") in *bash*) awk '{ if (/^# NAME/) { do {print substr($0, 3)} while (getline && !/^#----------/); } - }' $COMMAND | ${PAGER:-less} + }' "$COMMAND" | "${PAGER:-less}" ;; *python*) $COMMAND --help | ${PAGER:-less} # FIXME: not too pretty at the moment @@ -123,16 +118,16 @@ help|h|?|--help|-h) rose_usage echo echo "$ROSE_NS provides the following utilities:" - for U in $(cd $ROSE_HOME_BIN && ls $ROSE_NS-*); do - NAME=$(sed "s/^$ROSE_NS-\\(.*\\)\$/\1/" <<<$U) - ALIAS=$(get_alias $NAME) + for U in $(cd "$ROSE_HOME_BIN" && ls "$ROSE_NS-"*); do + NAME="$(sed "s/^$ROSE_NS-\\(.*\\)\$/\1/" <<<"$U")" + ALIAS="$(get_alias "$NAME")" if [[ -n $ALIAS ]]; then echo " $NAME" echo " (=$ALIAS)" else echo " $NAME" sed '1,/^# DESCRIPTION$/d;{s/^# / /;q;}' \ - $ROSE_HOME_BIN/$U + "$ROSE_HOME_BIN/$U" fi done } | ${PAGER:-less} @@ -143,7 +138,7 @@ help|h|?|--help|-h) if [[ $U == 'help' || $U == 'version' ]]; then continue fi - help_util $U || RC=$? + help_util "$U" || RC=$? done exit $RC :;; @@ -156,10 +151,10 @@ doc) PYTHONPATH=$(path_lead "${PYTHONPATH:-}" "$ROSE_HOME/lib/python") ROSE_UTIL=$UTIL export PATH PYTHONPATH ROSE_UTIL - let ns_len=${#ROSE_NS}+2 - for U in $(cd $ROSE_HOME_BIN && ls $ROSE_NS-*); do - NAME=$(sed "s/^$ROSE_NS-\\(.*\\)\$/\1/" <<<$U) - ALIAS=$(get_alias $NAME) + ns_len=$(( ${#ROSE_NS}+2 )) + for U in $(cd "$ROSE_HOME_BIN" && ls "$ROSE_NS-"*); do + NAME="$(sed "s/^$ROSE_NS-\\(.*\\)\$/\1/" <<<"$U")" + ALIAS="$(get_alias "$NAME")" if [[ -n $ALIAS ]]; then echo '==================================================' echo "${ROSE_NS} ${NAME} -> ${ROSE_NS} ${ALIAS}" @@ -171,8 +166,8 @@ doc) echo "$ROSE_NS $NAME" echo '==================================================' echo - PAGER=cat - help_util $(cut -c "${ns_len}-" <<<"${U}") + PAGER='cat' + help_util "$(cut -c "${ns_len}-" <<<"${U}")" echo fi done @@ -180,18 +175,18 @@ doc) :;; esac -COMMAND=$(dirname $0)/$ROSE_NS-$UTIL +COMMAND="$(dirname "$0")/$ROSE_NS-$UTIL" if [[ ! -f $COMMAND || ! -x $COMMAND ]]; then echo "$ROSE_NS: $UTIL: unknown utility. Abort." >&2 echo "Type \"$ROSE_NS help\" for a list of utilities." >&2 exit 1 fi if (($# > 0)) && [[ $1 == '--help' || $1 == '-h' ]]; then - help_util $UTIL + help_util "$UTIL" exit fi PATH=$(path_lead "${PATH:-}" "$ROSE_HOME_BIN") PYTHONPATH=$(path_lead "${PYTHONPATH:-}" "$ROSE_HOME/lib/python") ROSE_UTIL=$UTIL export PATH PYTHONPATH ROSE_UTIL -exec $COMMAND "$@" +exec "$COMMAND" "$@" diff --git a/bin/rose-edit b/bin/rose-edit index a67e510446..ae6b9e2e94 100755 --- a/bin/rose-edit +++ b/bin/rose-edit @@ -19,4 +19,4 @@ #------------------------------------------------------------------------------- # Alias of "rose config-edit". #------------------------------------------------------------------------------- -exec $(dirname $0)/rose-config-edit "$@" +exec "$(dirname "$0")/rose-config-edit" "$@" diff --git a/bin/rose-make-docs b/bin/rose-make-docs index 6bd2247f0b..6d10ce275a 100755 --- a/bin/rose-make-docs +++ b/bin/rose-make-docs @@ -107,13 +107,11 @@ SPHINX_PATH=sphinx . "${ROSE_HOME}/rose-version" # documentation root output directory DOCS_DIR="${ROSE_HOME}/doc" -# documentation output directory for this version -BUILD_DIR="${DOCS_DIR}/${ROSE_VERSION}" # glob for documentation formats within a rose version -DOC_FORMATS=!(*.html|doctrees) +DOC_FORMATS=(!(*.html|doctrees)) # is the virtualenv command available -if which virtualenv >/dev/null 2>&1; then +if command -v virtualenv >/dev/null 2>&1; then VENV_COMPLIANT=true else VENV_COMPLIANT=false @@ -124,8 +122,8 @@ VENV_MODE=false FORCE=false DEV_MODE=false DEBUG='' -BUILDS='' -SPHINX_OPTS='' +BUILDS=() +SPHINX_OPTS=() DEFAULT_ALIAS='doc' DEFAULT_VERSION= while [[ $# -gt 0 ]]; do @@ -143,7 +141,7 @@ while [[ $# -gt 0 ]]; do shift ;; --strict) - SPHINX_OPTS='SPHINXOPTS=-aEW' + SPHINX_OPTS+=('SPHINXOPTS=-aEW') shift ;; --debug) @@ -156,16 +154,16 @@ while [[ $# -gt 0 ]]; do shift ;; *) - BUILDS="${BUILDS} $1" + BUILDS+=("$1") shift ;; esac done -if [[ -z "${BUILDS}" ]]; then - BUILDS='html' +if [[ "${#BUILDS}" == 0 ]]; then + BUILDS=('html') fi # glob for documented rose versions -DOC_VERSIONS=!(*.html|doc|versions.json|"${DEFAULT_ALIAS}"|CHANGES.md|404.md|_config.yml) +DOC_VERSIONS=(!(*.html|doc|versions.json|"${DEFAULT_ALIAS}"|CHANGES.md|404.md|_config.yml)) venv-activate () { @@ -194,7 +192,7 @@ venv-destroy () { json_list () { # write out a bash array as a JSON list - echo -n "[\"$(local IFS=','; echo "$*" | sed 's/,/", "/g';)\"]" + local IFS=','; list="$*"; echo -n "[\"${list//,/\", \"}\"]"; } version_file () { @@ -203,11 +201,12 @@ version_file () { echo '{' # scrape filesystem for list of rose versions which have built docs - VERSIONS=( $(cd "${DOCS_DIR}"; echo $DOC_VERSIONS) ) + mapfile -t VERSIONS < <(cd "${DOCS_DIR}"; echo "${DOC_VERSIONS[@]}") for version in "${VERSIONS[@]}"; do # scrape filesystem to get list of formats this version is available in - formats=( $(cd "${DOCS_DIR}/${version}"; echo $DOC_FORMATS) ) - list=" \"${version}\": $(json_list ${formats[@]})" + mapfile -t formats \ + < <(cd "${DOCS_DIR}/${version}"; echo "${DOC_FORMATS[@]}") + list=" \"${version}\": $(json_list "${formats[@]}")" if [[ $version == "${VERSIONS[$(( ${#VERSIONS[@]} - 1 ))]}" ]]; then # JSON doesn't permit a comma after the last item in a collection echo "${list}" @@ -223,16 +222,16 @@ html_redirect () { SRC="$1" DEST="$2" - cat >"$2" << __HTML__ + cat >"$DEST" << __HTML__ Rose Documentation - +

If not automatically redirected, please click - Rose Documentation.

+ Rose Documentation.

__HTML__ @@ -256,7 +255,7 @@ if ! rose-check-software --doc >/dev/null; then venv-install elif ! "${FORCE}"; then echo 'Software required by the rose documentation builder is not -present (run `rose check-software --doc` for details). +present (run "rose check-software --doc" for details). For information on building a Python environment for the Rose documentation builder see the "Software Dependencies" section of the help page for this @@ -266,10 +265,10 @@ command.' >&2 fi # makefile argument to set the output directory for this build -SPHINX_OPTS="${SPHINX_OPTS} BUILDDIR=${DOCS_DIR}/${ROSE_VERSION}" +SPHINX_OPTS+=("BUILDDIR=${DOCS_DIR}/${ROSE_VERSION}") # run sphinx-build -if make ${DEBUG} -C "${SPHINX_PATH}" ${BUILDS} ${SPHINX_OPTS}; then +if make ${DEBUG} -C "${SPHINX_PATH}" "${BUILDS[@]}" "${SPHINX_OPTS[@]}"; then RET=0 # output file containing details of all versions and formats the # documentation has been built in (locally) for the version / format diff --git a/bin/rose-mpi-launch b/bin/rose-mpi-launch index f296cb0b9f..d1f79b9b6c 100755 --- a/bin/rose-mpi-launch +++ b/bin/rose-mpi-launch @@ -111,7 +111,7 @@ # DIAGNOSTICS # Return 0 on success, 1 or exit code of the launcher program on failure. #------------------------------------------------------------------------------- -. $(dirname $0)/../lib/bash/rose_init +. "$(dirname "$0")/../lib/bash/rose_init" rose_init rose_log # ------------------------------------------------------------------------------ @@ -135,14 +135,14 @@ while (($# > 0)); do shift 1 if [[ -n ${ROSE_LAUNCHER_ULIMIT_OPTS:-} ]]; then while getopts 'HST:ab:c:d:e:f:i:l:m:n:p:q:r:s:t:u:v:x:' \ - OPT $ROSE_LAUNCHER_ULIMIT_OPTS + OPT "$ROSE_LAUNCHER_ULIMIT_OPTS" do case "$OPT" in '?') err "ROSE_LAUNCHER_ULIMIT_OPTS=$ROSE_LAUNCHER_ULIMIT_OPTS" :;; *) - ulimit -$OPT ${OPTARG:-} || err \ + ulimit "-$OPT" "${OPTARG:-}" || err \ "ROSE_LAUNCHER_ULIMIT_OPTS=$ROSE_LAUNCHER_ULIMIT_OPTS" :;; esac @@ -189,14 +189,14 @@ else if (($# < 1)); then rose_usage 1 fi - if ! ROSE_COMMAND=$(type -P $1); then + if ! ROSE_COMMAND="$(type -P "$1")"; then err "$1: COMMAND not found" fi shift 1 fi ROSE_LAUNCHER_LIST=${ROSE_LAUNCHER_LIST:-$( \ - rose config --default= $ROSE_NS launcher-list)} + rose config --default= "$ROSE_NS" launcher-list)} export NPROC=${NPROC:-1} #------------------------------------------------------------------------------- @@ -211,7 +211,7 @@ ROSE_LAUNCHER_LIST="${ROSE_LAUNCHER_MPICH:-} $ROSE_LAUNCHER_LIST" if ! printenv ROSE_LAUNCHER >/dev/null; then ROSE_LAUNCHER= for LAUNCHER in $ROSE_LAUNCHER_LIST; do - if type -P $LAUNCHER >/dev/null; then + if type -P "$LAUNCHER" >/dev/null; then ROSE_LAUNCHER=$LAUNCHER break fi @@ -224,11 +224,11 @@ fi ROSE_LAUNCHER_BASE= if [[ -n $ROSE_LAUNCHER ]]; then # Path - if ! ROSE_LAUNCHER_PATH=$(type -P $ROSE_LAUNCHER); then + if ! ROSE_LAUNCHER_PATH="$(type -P "$ROSE_LAUNCHER")"; then err "ROSE_LAUNCHER: $ROSE_LAUNCHER: command not found" fi ROSE_LAUNCHER=$ROSE_LAUNCHER_PATH - ROSE_LAUNCHER_BASE=$(basename $ROSE_LAUNCHER) + ROSE_LAUNCHER_BASE="$(basename "$ROSE_LAUNCHER")" fi #------------------------------------------------------------------------------- @@ -239,9 +239,10 @@ if [[ -n $ROSE_COMMAND_FILE ]]; then err "ROSE_LAUNCHER not defined, command file not supported." fi ROSE_LAUNCHER_FILEOPTS=${ROSE_LAUNCHER_FILEOPTS:-$( \ - rose config --default= $ROSE_NS launcher-fileopts.$ROSE_LAUNCHER_BASE)} - eval "info 2 exec $ROSE_LAUNCHER $ROSE_LAUNCHER_FILEOPTS $@" - eval "exec $ROSE_LAUNCHER $ROSE_LAUNCHER_FILEOPTS $@" + rose config --default= "$ROSE_NS" \ + "launcher-fileopts.$ROSE_LAUNCHER_BASE")} + eval "info 2 exec $ROSE_LAUNCHER $ROSE_LAUNCHER_FILEOPTS $*" + eval "exec $ROSE_LAUNCHER $ROSE_LAUNCHER_FILEOPTS $*" else if [[ -n $ROSE_LAUNCHER_BASE ]]; then ROSE_LAUNCH_INNER= @@ -250,9 +251,9 @@ else fi # Options ROSE_LAUNCHER_PREOPTS=${ROSE_LAUNCHER_PREOPTS:-$(rose config -E \ - --default= $ROSE_NS launcher-preopts.$ROSE_LAUNCHER_BASE)} + --default= "$ROSE_NS" "launcher-preopts.$ROSE_LAUNCHER_BASE")} ROSE_LAUNCHER_POSTOPTS=${ROSE_LAUNCHER_POSTOPTS:-$(rose config -E \ - --default= $ROSE_NS launcher-postopts.$ROSE_LAUNCHER_BASE)} + --default= "$ROSE_NS" "launcher-postopts.$ROSE_LAUNCHER_BASE")} else ROSE_LAUNCH_INNER= ROSE_LAUNCHER_PREOPTS= @@ -261,8 +262,10 @@ else if ((ROSE_VERBOSITY >= 3)); then info 3 printenv printenv | sort - run ldd $ROSE_COMMAND || true 2>/dev/null + run ldd "$ROSE_COMMAND" || true 2>/dev/null fi + + # shellcheck disable=SC2086 info 2 exec \ $ROSE_LAUNCHER \ $ROSE_LAUNCHER_PREOPTS \ @@ -270,6 +273,7 @@ else "$ROSE_COMMAND" \ $ROSE_LAUNCHER_POSTOPTS \ "$@" + # shellcheck disable=SC2086 exec \ $ROSE_LAUNCHER \ $ROSE_LAUNCHER_PREOPTS \ diff --git a/bin/rose-slv b/bin/rose-slv index 9c4bc42c85..5041a76a4b 100755 --- a/bin/rose-slv +++ b/bin/rose-slv @@ -19,4 +19,4 @@ #------------------------------------------------------------------------------- # Alias of "rose suite-log". #------------------------------------------------------------------------------- -exec $(dirname $0)/rose-suite-log "$@" +exec "$(dirname "$0")/rose-suite-log" "$@" diff --git a/bin/rose-suite-init b/bin/rose-suite-init index 834e4bb792..5fb9de0f6f 100755 --- a/bin/rose-suite-init +++ b/bin/rose-suite-init @@ -20,4 +20,4 @@ # Deprecated. # Alias of "rose suite-run". #------------------------------------------------------------------------------- -exec $(dirname $0)/rose-suite-run "$@" +exec "$(dirname "$0")/rose-suite-run" "$@" diff --git a/bin/rose-suite-log-view b/bin/rose-suite-log-view index 9c4bc42c85..5041a76a4b 100755 --- a/bin/rose-suite-log-view +++ b/bin/rose-suite-log-view @@ -19,4 +19,4 @@ #------------------------------------------------------------------------------- # Alias of "rose suite-log". #------------------------------------------------------------------------------- -exec $(dirname $0)/rose-suite-log "$@" +exec "$(dirname "$0")/rose-suite-log" "$@" diff --git a/bin/rose-suite-stop b/bin/rose-suite-stop index 53af619543..9412cb9516 100755 --- a/bin/rose-suite-stop +++ b/bin/rose-suite-stop @@ -19,4 +19,4 @@ #------------------------------------------------------------------------------- # Alias of "rose suite-shutdown". #------------------------------------------------------------------------------- -exec $(dirname $0)/rose-suite-shutdown "$@" +exec "$(dirname "$0")/rose-suite-shutdown" "$@" diff --git a/bin/rose-task-hook b/bin/rose-task-hook index 58edcd260c..5043cca192 100755 --- a/bin/rose-task-hook +++ b/bin/rose-task-hook @@ -19,4 +19,4 @@ #------------------------------------------------------------------------------- # Alias of "rose suite-hook". #------------------------------------------------------------------------------- -exec $(dirname $0)/rose-suite-hook "$@" +exec "$(dirname "$0")/rose-suite-hook" "$@" diff --git a/bin/rose-test-battery b/bin/rose-test-battery index 2e191f5fcf..5a14528f70 100755 --- a/bin/rose-test-battery +++ b/bin/rose-test-battery @@ -48,8 +48,11 @@ # SEE ALSO # * `prove(1)` #------------------------------------------------------------------------------- -. $(dirname $0)/../lib/bash/rose_init -. $(dirname $0)/../lib/bash/rose_log +. "$(dirname "$0")/../lib/bash/rose_init" +. "$(dirname "$0")/../lib/bash/rose_log" + +set -eu + rose_init mkdir -p ~/.metomi if [[ $ROSE_HOME != . ]]; then @@ -62,7 +65,7 @@ if [[ -w 'lib/python/' ]]; then python3 -mcompileall -q 'lib/python/' fi if PROVE_OPTIONS=$(rose config t prove-options); then - exec prove $PROVE_OPTIONS -r "${@:-t}" + exec prove "$PROVE_OPTIONS" -r "${@:-t}" else if [[ -f /proc/cpuinfo ]]; then NPROC=$(grep -ic processor /proc/cpuinfo) diff --git a/bin/rose-tutorial b/bin/rose-tutorial index ab75255b95..490d159064 100755 --- a/bin/rose-tutorial +++ b/bin/rose-tutorial @@ -37,7 +37,7 @@ usage () { echo 'rose tutorial SUITE [DIR]' echo echo 'Available tutorial suites:' - ls -1 "${ROSE_HOME}/etc/tutorial/" | sed -e 's/^/ /' + find "./etc/tutorial/" -maxdepth 1 -exec basename {} \; | sed 's/^/ /' } api-key () { @@ -70,7 +70,7 @@ fi # Prompt user if directory exists. if [[ -d $DIRECTORY ]]; then - while read -p "Directory $DIRECTORY exists, overwrite it (y/n)? " usr + while read -rp "Directory $DIRECTORY exists, overwrite it (y/n)? " usr do case "${usr}" in [Yy]) @@ -89,9 +89,8 @@ rose_init 'rose_log' mkdir -p "$(dirname "${DIRECTORY}")" if run rsync -rLptgoD "${ROSE_HOME}/etc/tutorial/$1/" "${DIRECTORY}" \ --exclude='.validate'; then - for file in $(find "${DIRECTORY}" -type f); do - sed -i "s/DATAPOINT_API_KEY/$(api-key)/" $file - done + find "${DIRECTORY}" -type f \ + -exec sed -i "s/DATAPOINT_API_KEY/$(api-key)/" {} \; echo 'done' else echo diff --git a/bin/rosie-co b/bin/rosie-co index 2ccb31e18a..e4d40d4fe2 100755 --- a/bin/rosie-co +++ b/bin/rosie-co @@ -19,4 +19,4 @@ #------------------------------------------------------------------------------- # Alias of "rosie checkout". #------------------------------------------------------------------------------- -exec $(dirname $0)/rosie-checkout "$@" +exec "$(dirname "$0")/rosie-checkout" "$@" diff --git a/bin/rosie-copy b/bin/rosie-copy index 7899fc3c87..80254ced0e 100755 --- a/bin/rosie-copy +++ b/bin/rosie-copy @@ -19,4 +19,4 @@ #------------------------------------------------------------------------------- # Alias of "rosie create". #------------------------------------------------------------------------------- -exec $(dirname $0)/rosie-create "$@" +exec "$(dirname "$0")/rosie-create" "$@"