0

I've got this shell script which pushes a Docker image into a repo on AWS depending on if the branch is stage or production. the script throws [: unexpected operator

if [ -z "$GITLAB_PULL_REQUEST" ] || [ "$GITLAB_PULL_REQUEST" == "false" ]
then

  if [ "$GITLAB_BRANCH" == "staging" ] || \  # error refers to this condition statements
     [ "$GITLAB_BRANCH" == "production" ]
  then
    curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
    unzip awscli-bundle.zip
    ./awscli-bundle/install -b ~/bin/aws
    export PATH=~/bin:$PATH
    export AWS_ACCOUNT_ID=27472968600
    export AWS_ACCESS_KEY_ID=AKIAJKGN7TUK...
    export AWS_SECRET_ACCESS_KEY=M8G9Zei...
    eval $(aws ecr get-login --region eu-west-2 --no-include-email)
    export TAG=$GITLAB_BRANCH
    export REPO=$AWS_ACCOUNT_ID.dkr.ecr.eu-west-2.amazonaws.com
  fi

What is the correct way to do it in shell ?

Mark
  • 1,385
  • 3
  • 16
  • 29
  • In general, `[` does not support `==`; that's a `bash` extension. Use `=`, as your shell apparently expects. – chepner Mar 13 '20 at 14:47
  • Sorry, this supposed to be a bash script. Perhaps I don't know the difference between `sh` and `bash` as people sometimes use it interchangeably. – Mark Mar 13 '20 at 14:53
  • 1
    Don't worry. They weren't valid creds. – Mark Mar 13 '20 at 14:54
  • `sh` is more of a specification for a shell than a shell itself. On some machines, `/bin/sh` is a link or copy of `bash`; on others, it may be `dash` or some other shell. Basically, your script isn't being executed by `bash`. – chepner Mar 13 '20 at 14:56
  • If you write your script to adhere to the POSIX shell specification, then you can (mostly) rely on it working the same way, no matter what POSIX-compliant shell actually runs the script. – chepner Mar 13 '20 at 14:57
  • At the beginning of the script I've got `#!/bin/sh` that tells the interpreter to run it as a bash script, correct ? – Mark Mar 13 '20 at 15:07
  • No, that says to use `/bin/sh`, which as I said earlier, may not *be* `bash`. On a Ubuntu machine, for example, it's a link to `dash` instead. – chepner Mar 13 '20 at 15:08
  • I admit you've lost me. I'm on Ubuntu and so far all `sh` scripts I've ran using `sh filename.sh` and it ran just fine. I never heard of "dash". Gotta do further research about it. – Mark Mar 13 '20 at 15:16
  • You can choose if you want to make the script sh compatible it if you want to rub it with bash, but either way this duplicate has a lot of great info on this topic – that other guy Mar 13 '20 at 15:53
  • Dash, bash or sh I keep getting the same error. There must be something wrong with the syntax. – Mark Mar 13 '20 at 16:11
  • `readlink -f $(which sh)` gives me `/bin/dash` so how do you reckon I need to run the script ? – Mark Mar 13 '20 at 16:20

0 Answers0