Score:0

bash: line 146: [: missing `] - Gitlab CI/CD

cn flag

guys so I'm trying to make this if statement inside the script part of my CI/CD but it's giving a bash: line 146: [: missing ]` error which I'm not sure why is happening.

My script in CI/CD:

script:
        - >
            if [ $CI_COMMIT_TAG && $CI_COMMIT_REF_NAME =~ /^v/ ]; then 
                sshpass -e ssh -o StrictHostKeyChecking=no $DOCKER_SSH_USERNAME@$DOCKER_MACHINE "cd /home/$DOCKER_SSH_USERNAME && git clone $GIT_CLONE_URL && cd qcast-vosk-stt/ && chmod +x deploy.sh && ./deploy.sh -i $INSTANCES -e PRODUCTION";
                echo "Inside";
            else 
                sshpass -e ssh -o StrictHostKeyChecking=no $DOCKER_SSH_USERNAME@$DOCKER_MACHINE "cd /home/$DOCKER_SSH_USERNAME && git clone $GIT_CLONE_URL && cd qcast-vosk-stt/ && chmod +x deploy.sh && ./deploy.sh -i $INSTANCES -e DEVELOPMENT"; 
                echo "Elsed";
            fi

The pipeline error:

$ if [ $CI_COMMIT_TAG && $CI_COMMIT_REF_NAME =~ /^v/ ]; then  # collapsed multi-line command
/usr/bin/bash: line 146: [: missing `]'
Lorenz Keel avatar
gr flag
it should be `if [ condition1 ] && [ condition2 ]; then command1; else command2; fi`. So you shoul put a `]` before `&&` and also put a consistent check to variable `$CI_COMMIT_TAG`
bac0n avatar
cn flag
this is not what's wrong, single `[ ]` does not support binary operator `=~`. You have to use `[[ ]]`, it's perfectly fine to use `[[ $a && $b =~ pattern ]]` and `/ /` should be remove if they are not part of the pattern
DeadSec avatar
cn flag
@bac0n this worked :)
Score:0
gf flag
[[ $CI_COMMIT_TAG && $CI_COMMIT_REF_NAME =~ /^v/ ]];

should fix the problem. I don't see any issues with that warning, but it's always good to fix.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.