anordal / shellharden

The corrective bash syntax highlighter
Mozilla Public License 2.0
4.62k stars 129 forks source link

Parser got lost with `((10#${ver1[i]} > 10#${ver2[i]}));` #42

Closed AlexanderWillner closed 3 years ago

AlexanderWillner commented 3 years ago

Version:

Steps that will reproduce the problem?

  1. shellharden --check test.sh (see below)

What is the expected result?

What happens instead?

% shellharden --check test.sh

test.sh: Unexpected end of file
The file's end was reached without closing all sytactic scopes.
Either, the parser got lost, or the file is truncated or malformed.

Any additional information:

 % ./test.sh 
Expected to be 0 -> 0
Expected to be 1 -> 1
Expected to be 2 -> 2
#!/usr/bin/env bash

vercomp() {
  if [[ "$1" == "$2" ]]; then
    return 0
  fi
  local IFS=.
  local i ver1 ver2
  read -a ver1 <<<"$1"
  read -a ver2 <<<"$2"
  # fill empty fields in ver1 with zeros
  for ((i = ${#ver1[@]}; i < ${#ver2[@]}; i++)); do
    ver1[i]=0
  done
  for ((i = 0; i < ${#ver1[@]}; i++)); do
    if [[ -z "${ver2[i]}" ]]; then
      ver2[i]=0
    fi
    if ((10#${ver1[i]} > 10#${ver2[i]})); then
      return 1
    fi
    if ((10#${ver1[i]} < 10#${ver2[i]})); then
      return 2
    fi
  done
  return 0
}

vercomp 3.0 3
echo "Expected to be 0 -> $?"

vercomp 2.2 0.5
echo "Expected to be 1 -> $?"

vercomp 1.4 1.5
echo "Expected to be 2 -> $?"