0
votes

I wrote simple script to compare commit values in bash. Where as I am getting error.

export GIT_COMMIT=66597933406267ccb159c455b852480698b2c892
export LAST_COMMIT=66597933406267ccb159c455b852480698b2c892

 if [[ -z "$LAST_COMMIT"  || "$LAST_COMMIT" -eq "$GIT_COMMIT" ]]; then
    echo LAST_COMMIT='HEAD~1' > ~/Desktop/build.properties
 fi

commit.sh: line 3: [[: 66597933406267ccb159c455b852480698b2c892: value too great for base (error token is "66597933406267ccb159c455b852480698b2c892")

Both variable values are dynamic. for testing purpose, manually assigned.

May I know what could be the issue and best way to compare it in shell script.

1

1 Answers

0
votes

The -eq test is for numeric equality, and it requires decimal numbers. These inputs are hexadecimal strings. Instead of testing for numerical equality, test for string equality: there is no risk that one user will encode a number as, say, 00a3 and the other as a3 as both encodings came from a single "user" (the Git commit hash system).

Note that you can use the more generally applicable (non-bash-specific) [ POSIX tests for these:

if [ -z "LAST_COMMIT" -o "$LAST_COMMIT" = "$GIT_COMMIT" ]; then
    ...

which works in bash and plain old sh.