2014-03-12 41 views
29

在我的Ubuntu系統上,/usr/bin/ssh-copy-id包含一個好奇的一段代碼。它似乎檢查/bin/sh是一個「理智的外殼」。如果不是,它會嘗試用ksh重新運行該腳本。如果失敗了,它會拋出雙手並顯示錯誤消息。需要更少的昏暗的外殼

究竟是什麼檢查?特別是,if false^printf是做什麼的,爲什麼它只能在舊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" "[email protected]" 
    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 
+5

順便說一句,我認爲@三合一編輯提供的價值。標題本身看起來像用戶對shell的技術評論比較少,而不是被詢問的錯誤消息。 –

回答

21

原始Bourne支持^作爲管道運營商。這被丟棄在Korn shell(POSIX sh規範衍生的shell)中,因此它是Bourne中的一個功能,但不在POSIX sh中。

因此,此代碼測試pre-POSIX Bourne shell。

12

這部分內容適用於Solaris 10及更舊版本的腳本,其中/bin/sh不符合POSIX標準。請注意,後者不是一個bug,因爲POSIX不指定路徑應該是什麼。

/bin/sh(和/sbin/sh)在Solaris 10上可能是當前操作系統中唯一剩餘的仍然支持原始Bourne shell中此管道形式的shell。