When running the script detached from the repository content, the script attempts to execute grep
on dist/index.html
:
|
if [ "$result" -eq 1 ]; then |
|
echo "$tag not found in the index.html, please update the index.html file" |
|
(( status++ )) |
|
fi |
When this file is missing grep
exits with status 2
. Since this is performing an affirmative equality test (as opposed to "not equal to zero" or "greater than zero") this passes the test and proceeds without exiting. Additionally the use of
Also, on line 34 status
is being initialized as an indexed array but in it's assignment operation on line 39 it's only using the zeroth element as opposed to individual indexes. This lead to unintended behavior upon execution of lines 45 - 47:
|
if [[ "$value" -ge 1 ]]; then |
|
exit 1 |
|
fi |
This admittedly this may be a failure case considered NOTABUG
/WONTFIX
by the maintainers) but was raised just in case this is not the intended behavior. As to what the intended behavior is though, I'm not entirely clear. I noticed that grep
is being executed with the flags -Rq
. Consulting man 1 grep
on these options yields the following:
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. Also see the -s or --no-messages option.
-R, --dereference-recursive
Read all files under each directory, recursively. Follow all symbolic links, unlike -r.
Since all output is being redirected to /dev/null
i'm assuming -q
is being added to make the script a bit more robust. Is this correct? In the case of -R
i'm a bit more unclear. Since there is a file operand being used as a positional argument is this just to handle a use case where INDEXFILE
is a symbolic link?
As an example of the current state see the following (click to expand)
$ bash -x check-index.sh
+ set -o errexit
+ set -o nounset
+ set -o pipefail
+ LASTRELEASES=3
+ COUNTER=0
+ RELEASES=()
+ INDEXFILE=dist/index.html
++ curl -Ls https://dl.k8s.io/release/stable.txt
+ LATEST=v1.21.1
++ echo v1.21.1
++ cut -dv -d. -f1
+ LATESTMAJOR=v1
++ echo v1.21.1
++ cut -d. -f2
+ LATESTMINOR=21
+ RELEASES+=("$LATEST")
+ get_kubernetes_releases
+ echo 'Getting Kubernetes releases'
Getting Kubernetes releases
+ '[' 0 -lt 3 ']'
+ (( LATESTMINOR-- ))
++ curl -Ls https://dl.k8s.io/release/stable-1.20.txt
+ TEMP=v1.20.7
+ RELEASES+=("$TEMP")
+ (( COUNTER++ ))
+ '[' 1 -lt 3 ']'
+ (( LATESTMINOR-- ))
++ curl -Ls https://dl.k8s.io/release/stable-1.19.txt
+ TEMP=v1.19.11
+ RELEASES+=("$TEMP")
+ (( COUNTER++ ))
+ '[' 2 -lt 3 ']'
+ (( LATESTMINOR-- ))
++ curl -Ls https://dl.k8s.io/release/stable-1.18.txt
+ TEMP=v1.18.19
+ RELEASES+=("$TEMP")
+ (( COUNTER++ ))
+ '[' 3 -lt 3 ']'
+ echo 'Will validate the index.html using the following releases: ' v1.21.1 v1.20.7 v1.19.11 v1.18.19
Will validate the index.html using the following releases: v1.21.1 v1.20.7 v1.19.11 v1.18.19
+ check_index
+ status=()
+ for tag in "${RELEASES[@]}"
++ grep -Rq v1.21.1 dist/index.html
grep: dist/index.html: No such file or directory
++ echo 2
+ result=2
+ '[' 2 -eq 1 ']'
+ for tag in "${RELEASES[@]}"
++ grep -Rq v1.20.7 dist/index.html
grep: dist/index.html: No such file or directory
++ echo 2
+ result=2
+ '[' 2 -eq 1 ']'
+ for tag in "${RELEASES[@]}"
++ grep -Rq v1.19.11 dist/index.html
grep: dist/index.html: No such file or directory
++ echo 2
+ result=2
+ '[' 2 -eq 1 ']'
+ for tag in "${RELEASES[@]}"
++ grep -Rq v1.18.19 dist/index.html
grep: dist/index.html: No such file or directory
++ echo 2
+ result=2
+ '[' 2 -eq 1 ']'
[~/.local/bin]$ vim /tmp/ci.sh
[~/.local/bin]$ bash -x /tmp/ci.sh
+ set -o errexit
+ set -o nounset
+ set -o pipefail
+ LASTRELEASES=3
+ COUNTER=0
+ RELEASES=()
+ INDEXFILE=dist/index.html
++ curl -Ls https://dl.k8s.io/release/stable.txt
+ LATEST=v1.21.1
++ echo v1.21.1
++ cut -dv -d. -f1
+ LATESTMAJOR=v1
++ echo v1.21.1
++ cut -d. -f2
+ LATESTMINOR=21
+ RELEASES+=("$LATEST")
+ get_kubernetes_releases
+ echo 'Getting Kubernetes releases'
Getting Kubernetes releases
+ '[' 0 -lt 3 ']'
+ (( LATESTMINOR-- ))
++ curl -Ls https://dl.k8s.io/release/stable-1.20.txt
+ TEMP=v1.20.7
+ RELEASES+=("$TEMP")
+ (( COUNTER++ ))
+ '[' 1 -lt 3 ']'
+ (( LATESTMINOR-- ))
++ curl -Ls https://dl.k8s.io/release/stable-1.19.txt
+ TEMP=v1.19.11
+ RELEASES+=("$TEMP")
+ (( COUNTER++ ))
+ '[' 2 -lt 3 ']'
+ (( LATESTMINOR-- ))
++ curl -Ls https://dl.k8s.io/release/stable-1.18.txt
+ TEMP=v1.18.19
+ RELEASES+=("$TEMP")
+ (( COUNTER++ ))
+ '[' 3 -lt 3 ']'
+ echo 'Will validate the index.html using the following releases: ' v1.21.1 v1.20.7 v1.19.11 v1.18.19
Will validate the index.html using the following releases: v1.21.1 v1.20.7 v1.19.11 v1.18.19
+ check_index
+ status=()
+ for tag in "${RELEASES[@]}"
++ grep -Rq v1.21.1 dist/index.html
grep: dist/index.html: No such file or directory
++ echo 2
+ result=2
+ '[' 2 -eq 1 ']'
+ for tag in "${RELEASES[@]}"
++ grep -Rq v1.20.7 dist/index.html
grep: dist/index.html: No such file or directory
++ echo 2
+ result=2
+ '[' 2 -eq 1 ']'
+ for tag in "${RELEASES[@]}"
++ grep -Rq v1.19.11 dist/index.html
grep: dist/index.html: No such file or directory
++ echo 2
+ result=2
+ '[' 2 -eq 1 ']'
+ for tag in "${RELEASES[@]}"
++ grep -Rq v1.18.19 dist/index.html
grep: dist/index.html: No such file or directory
++ echo 2
+ result=2
+ '[' 2 -eq 1 ']'
$ echo $?
0
When line 37 is changed from `-eq` to `-ge` the behavior changes as follows (click to expand)
$ bash -x /tmp/ci.sh
+ set -o errexit
+ set -o nounset
+ set -o pipefail
+ LASTRELEASES=3
+ COUNTER=0
+ RELEASES=()
+ INDEXFILE=dist/index.html
++ curl -Ls https://dl.k8s.io/release/stable.txt
+ LATEST=v1.21.1
++ echo v1.21.1
++ cut -dv -d. -f1
+ LATESTMAJOR=v1
++ echo v1.21.1
++ cut -d. -f2
+ LATESTMINOR=21
+ RELEASES+=("$LATEST")
+ get_kubernetes_releases
+ echo 'Getting Kubernetes releases'
Getting Kubernetes releases
+ '[' 0 -lt 3 ']'
+ (( LATESTMINOR-- ))
++ curl -Ls https://dl.k8s.io/release/stable-1.20.txt
+ TEMP=v1.20.7
+ RELEASES+=("$TEMP")
+ (( COUNTER++ ))
+ '[' 1 -lt 3 ']'
+ (( LATESTMINOR-- ))
++ curl -Ls https://dl.k8s.io/release/stable-1.19.txt
+ TEMP=v1.19.11
+ RELEASES+=("$TEMP")
+ (( COUNTER++ ))
+ '[' 2 -lt 3 ']'
+ (( LATESTMINOR-- ))
++ curl -Ls https://dl.k8s.io/release/stable-1.18.txt
+ TEMP=v1.18.19
+ RELEASES+=("$TEMP")
+ (( COUNTER++ ))
+ '[' 3 -lt 3 ']'
+ echo 'Will validate the index.html using the following releases: ' v1.21.1 v1.20.7 v1.19.11 v1.18.19
Will validate the index.html using the following releases: v1.21.1 v1.20.7 v1.19.11 v1.18.19
+ check_index
+ status=()
+ for tag in "${RELEASES[@]}"
++ grep -Rq v1.21.1 dist/index.html
grep: dist/index.html: No such file or directory
++ echo 2
+ result=2
+ '[' 2 -ge 1 ']'
+ echo 'v1.21.1 not found in the index.html, please update the index.html file'
v1.21.1 not found in the index.html, please update the index.html file
+ (( status++ ))
+ for tag in "${RELEASES[@]}"
++ grep -Rq v1.20.7 dist/index.html
grep: dist/index.html: No such file or directory
++ echo 2
+ result=2
+ '[' 2 -ge 1 ']'
+ echo 'v1.20.7 not found in the index.html, please update the index.html file'
v1.20.7 not found in the index.html, please update the index.html file
+ (( status++ ))
+ for tag in "${RELEASES[@]}"
++ grep -Rq v1.19.11 dist/index.html
grep: dist/index.html: No such file or directory
++ echo 2
+ result=2
+ '[' 2 -ge 1 ']'
+ echo 'v1.19.11 not found in the index.html, please update the index.html file'
v1.19.11 not found in the index.html, please update the index.html file
+ (( status++ ))
+ for tag in "${RELEASES[@]}"
++ grep -Rq v1.18.19 dist/index.html
grep: dist/index.html: No such file or directory
++ echo 2
+ result=2
+ '[' 2 -ge 1 ']'
+ echo 'v1.18.19 not found in the index.html, please update the index.html file'
v1.18.19 not found in the index.html, please update the index.html file
+ (( status++ ))
+ for value in "${status[@]}"
+ [[ 4 -ge 1 ]]
+ exit 1
Again, referencing man 1 grep
:
EXIT STATUS
Normally the exit status is 0 if a line is selected, 1 if no lines were selected, and 2 if an error occurred. However, if the -q or --quiet or --silent is used and a line is selected, the exit status is 0 even if an error occurred.
It may be useful to leave -eq 1
in place on line 37 but also add a second conditional branch checking for -eq 2
. That said,
adding the conditional branch would only make sense if -q
was removed from the grep
execution.
Whether or not this is desirable is entirely dependent on the intention of the developers.