【问题标题】:Less dimwitted shell required需要更少的笨蛋壳
【发布时间】:2014-04-16 22:27:20
【问题描述】:

在我的 Ubuntu 系统上,/usr/bin/ssh-copy-id 在顶部包含一段奇怪的代码。似乎检查/bin/sh 是一个“健全的外壳”。如果不是,它会尝试使用ksh 重新运行脚本。如果失败,它会举手并显示错误消息。

它到底在检查什么?特别是,if false ^ printf 做了什么,为什么它只在旧 shell 中触发?古代的 shell 曾经有一个 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

【问题讨论】:

  • 顺便说一句,我认为@triplee 的编辑提供了价值。就其本身而言,标题看起来像是用户对 shell 的非技术性评论,而不是正在询问的错误消息。

标签: linux shell ubuntu sh


【解决方案1】:

原始 Bourne 支持 ^ 作为管道运算符。这是在 Korn shell(POSIX sh 规范从中派生)中删除的,因此是 Bourne 中可用但在 POSIX sh 中不可用的功能。

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

【讨论】:

    【解决方案2】:

    这部分是为了让脚本在 /bin/sh 不符合 POSIX 的 Solaris 10 及更早版本上运行。请注意,后者不是错误,因为 POSIX 没有指定 sh 路径应该是什么。

    Solaris 10 上的/bin/sh(和/sbin/sh)可能是当前操作系统中仅存的仍支持原始 Bourne shell 中出现的这种管道形式的 shell。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-28
      • 1970-01-01
      • 2012-02-24
      • 2017-01-08
      • 2019-08-10
      • 2013-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多