Skip to content

Bug/fixing unbound variable 63 #64

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

Merged
merged 1 commit into from
Jun 27, 2024

Conversation

justin-octo
Copy link
Contributor

Using -z in an if statement causes an unbound variable error if set -eu is declared but the variable has not been declared. Changing thing to -n which checks if the variable is declared and/or undefined. undefined is an empty string like bob=""
undeclared is if it has never been declared at all

Resolves #63

Using -z in an if statement causes an unbound variable error if set -eu is declared but the variable has not been declared.  Changing thing to -n which checks if the variable is declared and/or undefined.
undefined is an empty string like bob=""
undeclared is if it has never been declared at all
@justin-octo justin-octo requested a review from brikis98 as a code owner June 27, 2024 18:17
@justin-octo justin-octo changed the title fixing unbound variable Bug/fixing unbound variable 63 Jun 27, 2024
@justin-octo
Copy link
Contributor Author

@gcagle3 not sure how we missed this.

@justin-octo
Copy link
Contributor Author

justin-octo commented Jun 27, 2024

Tested this with a simple script:

#!/bin/bash

set -eu

if [[ -z $bob ]]; then
  echo "It is there with -z"
else
  echo "Fail on -z"
fi

if [[ -n $bob ]]; then
  echo "It is there with -n"
else
  echo "Fail on -n"
fi

echo "Bob is ${bob}"

Which fails with these results:

./testingif.sh: line 5: bob: unbound variable

If you add in a line into the script above the if statements:

bob=""

Then you get this result (with no error message, and the script completes):

It is there with -z
Fail on -n
Bob is 

This indicates we need -n in the if statement to properly handle when the variable GRUNTWORK_BASH_COMMONS_IMDS_VERSION is undeclared.

@gcagle3
Copy link
Contributor

gcagle3 commented Jun 27, 2024

@gcagle3 not sure how we missed this.

Excellent catch, thank you for creating this PR! I think missing this is a side effect of the limitations of the test cases. I'm going to create a task to research improving that situation to help avoid future issues being missed.

@gcagle3 gcagle3 merged commit 3fc19af into gruntwork-io:master Jun 27, 2024
2 of 3 checks passed
@gcagle3
Copy link
Contributor

gcagle3 commented Jun 27, 2024

This has been merged and included in release v0.2.1. We have also updated the release notes for v0.2.0 to include a warning about the bug.

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.

If statement in IMDSv2 check needs to check for undeclared variable
2 participants