Skip to content

BUG: Fix all shellcheck error-level findings in Scripts/ (refs #397)#1974

Merged
hjmjohnson merged 6 commits into
ANTsX:masterfrom
hjmjohnson:fix/shellcheck-errors-issue-397
Jun 8, 2026
Merged

BUG: Fix all shellcheck error-level findings in Scripts/ (refs #397)#1974
hjmjohnson merged 6 commits into
ANTsX:masterfrom
hjmjohnson:fix/shellcheck-errors-issue-397

Conversation

@hjmjohnson

Copy link
Copy Markdown
Collaborator

Eliminates all 83 shellcheck -S error findings (4 distinct rules) in Scripts/, fixing several latent bugs along the way. First step of the cleanup discussed in #397; warnings/info-level findings are not addressed here.

Each shellcheck rule is its own commit so individual fixes can be reverted or cherry-picked independently.

Per-commit breakdown
Commit Rule Fixes Notes
BUG: SC1037 … SC1037 ×2 sliceBySliceOperation.sh reading $10 / $11 Bash parses bare $10 as ${1}0; the script silently stuffed garbage into PARAMETERS[5,6] whenever it was invoked with ≥10 args. Real bug.
BUG: SC1087 … SC1087 ×6 antsMultivariateTemplateConstruction{,2}.sh, buildtemplateparallel.sh, directlabels.sh Two patterns: (a) echo $files[1] where files is a scalar from backtick ls (the [1] was dead text appended to the whole string) — index removed; (b) echo "$TEMPLATES[$i] was not created" where TEMPLATES is a real array — braces added so the right element is printed in the error message.
BUG: SC2145 … SC2145 ×11 antsMultivariateTemplateConstruction{,2}.sh (a) echo "prefix ${arr[@]}"${arr[*]} (echo wants one space-joined string). (b) mv ${tmpdir}/selection/${TEMPLATES[@]} ${currentdir}/ was a real bug — the prefix only glued to the first element, so subsequent files were picked up from cwd instead of ${tmpdir}/selection/. Replaced with an explicit for loop.
BUG: SC2068 … SC2068 ×64 14 scripts Mechanical ${arr[@]}"${arr[@]}" everywhere it was unquoted. Done with a perl in-place regex using negative lookbehind/ahead so already-quoted expansions are left alone. Protects against filenames-with-spaces being re-split by IFS.
Verification
  • shellcheck -S error Scripts/*.sh returns 0 lines after the four commits (was 83 lines before).
  • bash -n syntax check passes on every modified file.
  • No behavior change is intended in the SC2068 commit (pure quoting). The SC1087/SC2145 commits include three genuine bug fixes (positional-arg parse, error-message array index, mv cross-directory move) which alter observable behavior only in the broken-case direction.
What's NOT in this PR
  • 497 shellcheck warnings (SC2206/SC2034/SC1083/SC2207/SC2124/SC2164/…) — these are the precondition cleanup needed before set -euo pipefail adoption can be safely attempted.
  • set -e adoption itself — only 1 of 46 scripts uses strict mode today. That's a separate, per-script PR with maintainer regression testing per the discussion in bash scripts don't catch errors #397.
  • A CI lint gate. A reasonable next step is to add shellcheck -S error Scripts/*.sh to CI to keep the 0-error invariant.

Refs: #397

sliceBySliceOperation.sh used bare $10 and $11 to read the 10th and
11th positional parameters. Bash parses $10 as ${1}0 (first arg
followed by literal '0'), not the 10th argument; the script silently
populated PARAMETERS[5] and PARAMETERS[6] with garbage whenever it
was invoked with 10+ arguments.

Use the required ${10} and ${11} braced form so the intended
positionals are read.

Refs: issue ANTsX#397.
Two patterns fixed:

1. "echo $files[1]" in antsMultivariateTemplateConstruction.sh,
   antsMultivariateTemplateConstruction2.sh, buildtemplateparallel.sh,
   and directlabels.sh. $files is a scalar from backtick ls, not an
   array; $files[1] expands to the whole string followed by literal
   '[1]'. The trailing index was dead and is removed.

2. "$TEMPLATES[$i]" inside an echo error message in both template
   construction scripts. TEMPLATES is a genuine array (the surrounding
   test uses ${TEMPLATES[$i]}). The unbraced form printed the first
   element plus literal '[$i]', mis-identifying which template failed.

Refs: issue ANTsX#397.
Two patterns fixed in antsMultivariateTemplateConstruction{,2}.sh:

1. echo "prefix ${arr[@]}" — ${arr[@]} inside a quoted string
   glues each element with adjacent text on its ends, producing a
   different word list than intended. Changed to ${arr[*]} so the
   array elements are space-joined into one argument as the echo
   call already assumes.

2. "mv ${tmpdir}/selection/${TEMPLATES[@]} ${currentdir}/" — the
   prefix glued only to the first array element; subsequent elements
   were taken from the current working directory rather than the
   selection subdir. Replaced with an explicit loop that moves each
   template file from the selection subdirectory.

Refs: issue ANTsX#397.
Add double quotes around every flagged ${arr[@]} expansion across the
Scripts/ tree. The unquoted form re-splits each element on IFS, so any
filename, prefix, or option string containing whitespace is silently
broken into multiple shell arguments before being handed to the
downstream ANTs binary or rm/mv/echo.

Files touched:
  antsAtroposN4.sh, antsBrainExtraction.sh, antsCookTemplatePriors.sh,
  antsCorticalThickness.sh, antsJointLabelFusion.sh,
  antsJointLabelFusion2.sh, antsLongitudinalCorticalThickness.sh,
  antsLongitudinalJointLabelFusion.sh,
  antsMultivariateTemplateConstruction.sh,
  antsMultivariateTemplateConstruction2.sh,
  antsRegistrationSpaceTime.sh, directlabels.sh,
  shapeupdatetotemplate.sh, sliceBySliceOperation.sh

This is a mechanical ${arr[@]} -> "${arr[@]}" substitution applied
only to previously-unquoted occurrences; no other behaviour changes.

Refs: issue ANTsX#397.
@hjmjohnson hjmjohnson requested a review from cookpa May 20, 2026 12:24
@hjmjohnson

Copy link
Copy Markdown
Collaborator Author

@gdevenyi This was inspired by your issue #397. The shell check tool lists several other warnings and suggestions, but this PR only addresses the perceived error cases (errors under conditions that may not exist, like spaces in filenames).

@hjmjohnson hjmjohnson marked this pull request as ready for review May 20, 2026 12:25
@gdevenyi

Copy link
Copy Markdown
Contributor

This will help a little bit but its a proper strict mode which I think is important for not having scripts run off and continue when errors are encountered.

@hjmjohnson

Copy link
Copy Markdown
Collaborator Author

This will help a little bit but its a proper strict mode which I think is important for not having scripts run off and continue when errors are encountered.

This is just one step to keep the PR not too big.

@gdevenyi

Copy link
Copy Markdown
Contributor

This is just one step to keep the PR not too big.

Fair enough. I don't have review privileges here, but I approve 👍

@cookpa

cookpa commented May 22, 2026

Copy link
Copy Markdown
Member

Thanks @hjmjohnson @gdevenyi I will review ASAP

@cookpa cookpa left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a minor change in the longitudinal thickness script, to make usage format correctly.

Comment thread Scripts/antsCorticalThickness.sh
Comment thread Scripts/antsMultivariateTemplateConstruction.sh
Comment thread Scripts/antsAtroposN4.sh
Comment thread Scripts/antsLongitudinalCorticalThickness.sh Outdated
Comment thread Scripts/antsLongitudinalCorticalThickness.sh Outdated
@cookpa cookpa self-requested a review June 5, 2026 15:13
@hjmjohnson hjmjohnson merged commit fc7557f into ANTsX:master Jun 8, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants