1

I have this script:

#!/usr/bin/env sh

# note: we must use sh instead of bash, it's more cross-platform

set -e;

if [[ "$skip_postinstall" == "yes" ]]; then   # TODO rename 'skip_postinstall' to something more specific
    echo "skipping postinstall routine.";
    exit 0;
fi

export FORCE_COLOR=1;
export skip_postinstall="yes";   # TODO rename 'skip_postinstall' to something more specific

mkdir -p "$HOME/.oresoftware/bin" || {
  echo "Could not create .oresoftware dir in user home.";
  exit 1;
}

(
  echo 'Installing run-tsc-if on your system.';
  curl  -H 'Cache-Control: no-cache' -s -S -o- 'https://raw.githubusercontent.com/oresoftware/run-tsc-if/master/install.sh' | bash || {
     echo 'Could not install run-tsc-if on your system. That is a problem.';
     exit 1;
  }
) 2> /dev/null


if [[ "$(uname -s)" != "Darwin" ]]; then
   exit 0;
fi

if [[ ! -f "$HOME/.oresoftware/bin/realpath" ]]; then
  (
    curl --silent -o- 'https://raw.githubusercontent.com/oresoftware/realpath/master/assets/install.sh' | bash || {
       echo "Could not install realpath on your system.";
       exit 1;
    }
  )
fi

# the end of the postinstall script

when I run it I get:

./assets/postinstall.sh: 7: ./assets/postinstall.sh: [[: not found
Installing run-tsc-if on your system.

 => Installing 'run-tsc-if' on your system.
 => run-tsc-if download/installation succeeded.

./assets/postinstall.sh: 29: ./assets/postinstall.sh: [[: not found
./assets/postinstall.sh: 33: ./assets/postinstall.sh: [[: not found

but if I replace the double [[ brackets with single brackets, I get:

./postinstall.sh: 7: [: unexpected operator

which just comes from:

if [ "$skip_postinstall" == "yes" ]; then  
    echo "skipping postinstall routine.";
    exit 0;
fi

umm what do I do? I tried using test instead of [ and [[ and that didn't work either.

2 Answers2

3

[[ is the "extended test" originating from ksh and also supported by bash/zsh so it should not recognize them. Additionally the == operator is not POSIX, = is the shell test operator for string comparison.

These are two very common bashisms.

jesse_b
  • 37,005
  • shame it doesn't say == not recognized instead of [ not recognized :( but if figured it out – Alexander Mills Feb 24 '20 at 20:37
  • @AlexanderMills: it does say "unexpected operator" – jesse_b Feb 24 '20 at 20:39
  • @AlexanderMills, If that's dash, it does say dash: 1: [: foo: unexpected operator for [ foo == bar ], so it seems it would print the first argument, but I guess it's empty in your case. I don't know why it works like that, with the three-argument test, the middle one is the important one. – ilkkachu Feb 24 '20 at 21:02
  • not really, [[ is a ksh88 feature. It is intentionally not in POSIX. – schily Feb 24 '20 at 21:11
2

I think it's because sh cannot handle == comparison, so it needs to be:

if [ "$skip_postinstall" = "yes" ]; then  
    echo "skipping postinstall routine.";
    exit 0;
fi

not this

if [ "$skip_postinstall" == "yes" ]; then  
    echo "skipping postinstall routine.";
    exit 0;
fi

crikey