sdgfsdh
sdgfsdh

Reputation: 37045

Bash script has unexpected token `;;'

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.

Upvotes: 0

Views: 258

Answers (3)

clt60
clt60

Reputation: 63912

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}".

Upvotes: 0

John Zwinck
John Zwinck

Reputation: 249153

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?

Upvotes: 4

William Pursell
William Pursell

Reputation: 212248

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.)

Upvotes: 3

Related Questions