需要更少笨重的外壳

Joh*_*ica 29 linux shell ubuntu sh

在我的Ubuntu系统上,/usr/bin/ssh-copy-id顶部包含一段奇怪的代码.它似乎检查这/bin/sh是一个"理智的外壳".如果不是,它会尝试重新运行脚本ksh.如果失败,它会抛出手并显示错误消息.

它究竟在检查什么?特别是,它if false ^ printf做了什么,为什么它只在旧壳中触发?古代的炮弹是用来拥有一个XOR算子,还是什么?

#!/bin/sh
# ...
# check that we have something mildly sane as our shell, or try to find something better
if false ^ printf "%s: WARNING: ancient shell, hunting for a more modern one... " "$0"
then
  SANE_SH=${SANE_SH:-/usr/bin/ksh}
  if printf 'true ^ false\n' | "$SANE_SH"
  then
    printf "'%s' seems viable.\n" "$SANE_SH"
    exec "$SANE_SH" "$0" "$@"
  else
    cat <<-EOF
        oh dear.

          If you have a more recent shell available, that supports \$(...) etc.
          please try setting the environment variable SANE_SH to the path of that
          shell, and then retry running this script. If that works, please report
          a bug describing your setup, and the shell you used to make it work.

        EOF
    printf "%s: ERROR: Less dimwitted shell required.\n" "$0"
    exit 1
  fi
fi
Run Code Online (Sandbox Code Playgroud)

Cha*_*ffy 22

原Bourne支持^管道操作员.这被删除在Korn shell中(POSIX sh规范派生自那里),因此是Bourne中可用但不在POSIX中的功能.

因此,此代码测试pre-POSIX Bourne shell.


jll*_*gre 12

这部分是脚本在Solaris 10及更旧版本上工作的,/bin/sh而不符合POSIX标准.请注意,后者不是错误,因为POSIX没有指定sh应该是什么路径.

/bin/sh(和/sbin/sh)在Solaris 10上可能是当前操作系统中唯一仍支持这种形式的管道的剩余shell,它出现在最初的Bourne shell中.