sh(不是 bash)无法识别单 [ 或双 [[ 括号?

sh(不是 bash)无法识别单 [ 或双 [[ 括号?

我有这个脚本:

#!/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

当我运行它时我得到:

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

但如果我用单括号替换双 [[ 括号,我得到:

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

这只是来自:

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

嗯我该怎么办?我尝试使用andtest代替,但这也不起作用。[[[

答案1

[[是源自 ksh 的“扩展测试”,也受 bash/zsh 支持,因此它不应该认出他们。另外该==运算符不是 POSIX,=而是用于字符串比较的 shell 测试运算符。

这是两个很常见的巴什主义

答案2

我认为这是因为 sh 无法处理 == 比较,所以它需要是:

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

不是这个

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

克里基

相关内容