0

I have a Bash command that is generated by a Python script:

platform=$(case $(uname) in
  "Linux") echo "linux-x86_64" ;;
  "Darwin") echo "darwin64-x86_64-cc" ;;
  *) echo "Unknown" esac);
cp -r $SRCDIR $OUT && cd $OUT && ./Configure shared $platform --prefix=$OUT/build --openssldir=$OUT/build/openssl && make && make install

When I try to execute it, I get the following error:

unexpected token `;;'

What have I done wrong here?

I am on the latest macOS.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245

3 Answers3

4

A much clearer and more efficient way to write the code would be:

case $(uname) in
  "Linux") platform="linux-x86_64" ;;
  "Darwin") platform="darwin64-x86_64-cc" ;;
  *) platform="Unknown" ;;
esac

Having the result of echo captured by $() is something of a shell anti-pattern. Why write to stdout and read from stdin when you can simply use the text directly?

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 2
    I am debating down voting or deleting my own answer! Not sure deleting is right, since the mismatched closing paren could be a common source of error, but this solution is far more appropriate for this case. – William Pursell Apr 25 '17 at 13:38
3

The closing ) on the "Linux" case is terminating the process substitution. Since it's bash, just use matching parens in the case:

platform=$(case $(uname) in
  ("Linux") echo "linux-x86_64" ;;
  ("Darwin") echo "darwin64-x86_64-cc" ;;
  (*) echo "Unknown";;
  esac
)

(Also, note that I had to add ;; after the default case.)

William Pursell
  • 204,365
  • 48
  • 270
  • 300
0

I would use something like:

get_platform() {
        case "${1,,}" in
                darwin*) echo darwin64...  ;;
                freebsd*) echo freebsd...  ;;
                linux*) echo linux-x86_64.. ;;
                cygwin*) echo cyg...  ;;
                *) return 1;;
        esac
        return 0
}

platform=$(get_platform "$OSTYPE" || get_platform $(uname))
echo "${platform:-Unknown}"

Double check - bash stores the operating system in the $OSTYPE variable. In case of fail (other shells), it still trying to use the uname.

In the case of undetected OS, the final decision, is populated to the main script (return 1) so you can check and adjust it as you want, like echo "${platform:-Unknown}".

clt60
  • 62,119
  • 17
  • 107
  • 194