"Life is all about sharing. If we are good at something, pass it on." - Mary Berry

A && B || C is not the same as if-then-else


Categories: DevOps

I have been thought that if-then-else statement can be written in one line by using A && B || C but I was wrong.

To speed up my Drone CI time, I configured the static-check step to only run when there are some .go files have been changed:

1git --no-pager diff --name-only ${DRONE_COMMIT_LINK##*/} | grep -q "\.go$" && golangci-lint run --deadline 2m -v ./... || true

I was thought that A && B || C is the same as:

1if [[ A ]]; then
2    B
4    C

But it’s not. The pipeline is still success even that step is failed:

1+ git --no-pager diff --name-only 1098f1323249f4560a706501a9a76139bb149551...cac8967252fe374561e37b81c035d428d3fff163 | grep -q "\.go$" && golangci-lint run --deadline 2m -v ./... || true
2fatal: Invalid symmetric difference expression 1098f1323249f4560a706501a9a76139bb149551...cac8967252fe374561e37b81c035d428d3fff163

Actually, A && B || C means that:

So, A && B || C will be the same as if [[ A ]]; then B; else C; fi if B is always success. My problem is golangci-lint returned an error but true is run -> the pipeline still success.

It can be fixed by switching back to use if:

1if git --no-pager diff --name-only ${DRONE_COMMIT_LINK##*/} | grep -q "\.go$"; then golangci-lint run --deadline 2m -v ./...; fi

Tags: bash shell

Edit on GitHub

Related Posts: