-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bash 3.x compatibility; avoid mapfile #48
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good! Would be awesome to get @toolmantim 👀 on this also.
@pda bash is always beyond my bash! I think we might have to remove the integration test for now, on our new untrusted VM world |
Removing that acceptance test in #49 |
Thanks for jumping on this fix Paul, I tried running this this morning with your patch and got:
this is what my pipeline config looks like:
Any idea why account_ids is unbound? Edit: I added my account ID and it ran successfully. |
Thanks @pda - and very timely. Turned out much of my day was working on pipelines and this helped speed up the change/test cycle time by an amazing amount by testing local. Other than needing to set |
Interesting… it appears bash 3.x treats for ver in 3.2 4.4 5.0; do
docker run --rm bash:$ver bash -u -c 'echo $BASH_VERSION; a=(); echo ${a[*]} ok';
done
# 3.2.57(1)-release
# bash: a[*]: unbound variable
# 4.4.23(1)-release
# ok
# 5.0.17(1)-release
# ok I've pushed a change to check for emptiness in a different way: - if [[ -z ${account_ids[*]} ]]; then
+ if [[ ${#account_ids[@]} -eq 0 ]]; then The semantics are slightly different if the array contains a single empty string: set -u
a=("")
echo "count: ${#a[@]}"
if [[ -z "${a[*]}" ]]; then echo "empty"; else echo "non-empty"; fi
# count: 1
# empty But I don't think that's a problem for us 🤞🏼 |
Actually I ended up using a combination of the two approaches, to keep the old behaviour but avoid the bash 3.x error: - if [[ -z ${account_ids[*]} ]]; then # before this PR
- if [[ ${#account_ids[@]} -eq 0 ]]; then # earlier in this PR
+ if [[ ${#account_ids[@]} -eq 0 || -z "${account_ids[*]}" ]]; then # combo It only reaches the |
mapfile was introduced in bash 4.x macOS ships with bash 3.x Take advice from shellcheck: https://github.com/koalaman/shellcheck/wiki/SC2207
bash 3.x treats ${foo[*]} on an empty array as an unbound variable. Newer bash 4.x and 5.x does not have this problem. We can use ${#foo[@]} instead, to check if the array is empty.
e2ed9bc
to
5ac470d
Compare
(also, rebased onto #49 to get rid of the already-failing acceptance test; should give us a ✅ on this PR) |
Use of
mapfile
was introduced in da2472f (to fix ashellcheck
) and 509678c (to support awscli v2).mapfile
was introduced in bash 4.x, but there are many systems that ship with bash 3.x (perhaps just macOS, but there's a lot of macOS systems 😅).This patch takes advice from shellcheck at https://github.com/koalaman/shellcheck/wiki/SC2207 to use bash 3.x compatible
read -a
to remove the dependency onmapfile
while also avoiding the previousshellcheck
warnings.Fixes #46, closes #47.
Ping: @pdbreen @ryansdwilson @kkolk