2

I've got a conditional if statement with multiple comparisons that keeps failing. This was written for macOS and I am trying to get to run on Ubuntu 18.

if [ "$SERVER_ENV" == "dev" ] || [ "$SERVER_ENV" == "test" ] || [ "$SERVER_ENV" == "live" ]; then       
    echo "Checking out master branch"
    git checkout master
else
    echo "Checking out multidev branch: $SERVER_ENV"
    git checkout $SERVER_ENV
fi

The error I recieve is [: test: unexpected operator for the first line.

mrpatg
  • 10,001
  • 42
  • 110
  • 169
  • 1
    `==` isn't a POSIX-defined operator for `[`; it's a bash-and-ksh extension. – Charles Duffy Oct 31 '19 at 15:58
  • 1
    The standard-compliant string comparison operator is `=`, not `==`. And if you want bash extensions, run your script with *bash*, not `sh`. (That means no `#!/bin/sh`, and no `sh scriptname`). – Charles Duffy Oct 31 '19 at 15:59
  • 1
    Alternately, switch from `if` to `case`; all POSIX-compliant shells will support `case $SERVER_ENV in dev|test|live) ...master code...;; *) ...multidev code... ;; esac` – Charles Duffy Oct 31 '19 at 16:00
  • 1
    (btw, all-caps variable names are used for variables meaningful to the shell itself, whereas names with at least one lowercase character are reserved for application use and guaranteed not to inadvertently modify shell behavior, so consider using `server_env` instead of `SERVER_ENV` in the future; see https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html, keeping in mind that shell and environment variables share a single namespace). – Charles Duffy Oct 31 '19 at 16:02
  • Which shell you are using? If I run your code in `bash` without both _git_ lines, it works. – John Goofy Oct 31 '19 at 16:04

0 Answers0