Bash one liner if statement is not working

Hi,

The following statement cause github action to exit:

[["${#TAG_LIST[@]}" -ne 3 ]] && echo "$RELEASE_VERSION is not a valid semantic version" && exit 1

The following statement passes

if [["${#TAG_LIST[@]}" -ne 3 ]]; then
  echo "$RELEASE_VERSION is not a valid semantic version"
  exit 1
fi

Is there any specific reason for that ?

Best regards,

Jonathan.

Hi @jojoooo1 , 

When I try the second method, the workflow run also exit. Could you please share your whole workflow yml content here? 

Is the result of 

"${#TAG_LIST[@]}" -ne 3

 changed in your side? 

Hi @yanjingzhu 

I wrote a bit quickly you will find below the variable details:

RELEASE_VERSION=2.3.1
TAG_LIST=(`echo $RELEASE_VERSION | tr '.' ' '`)

# this will pass
if [["${#TAG_LIST[@]}" -ne 3 ]]; then
  echo "$RELEASE_VERSION is not a valid semantic version"
  exit 1
fi

# this will not pass
[["${#TAG_LIST[@]}" -ne 3 ]] && echo "$RELEASE_VERSION is not a valid semantic version" && exit 1

@jojoooo1
Thank you for sharing your detail bash scripts here. Based on my test ,

TAG_LIST=(`echo $RELEASE_VERSION | tr '.' ' '`)

just returns 2, this cause the result of if condition to false, so exit 1 will not execute when you use if.

But when you connect your commands with && , exit 1 will always be executed.
Could you please change that line to the next syntax?

TAG_LIST=$(echo $RELEASE_VERSION | tr '.' ' ')

Hi @yanjingzhu,

My response from days ago seems to not have been registered :(. Anyway basically my IF condition should have works since it was measuring the length of the TAG_LIST variable.

I was able to solve it using this which make sens from an array stand of point by using the two parentheses:

TAG_LIST=($(echo $NEW_RELEASE_VERSION | tr '.' ' '))

All the best,

Jonathan.

1 Like