Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Less dimwitted shell required

On my Ubuntu system, /usr/bin/ssh-copy-id contains a curious piece of code at the top. It appears to check that /bin/sh is a "sane shell". If it's not, it tries to re-run the script with ksh. If that fails, it throws up its hands and displays an error message.

What exactly is it checking for? In particular, what does if false ^ printf do, and why is it only triggered only in old shells? Did ancient shells use to have an XOR operator, or what?

#!/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
like image 466
John Kugelman Avatar asked Mar 12 '14 16:03

John Kugelman


2 Answers

Original Bourne supported ^ as the pipe operator. This was dropped in the Korn shell (from which the POSIX sh spec derived), and is thus a feature available in Bourne but not in POSIX sh.

Thus, this code tests for pre-POSIX Bourne shells.

like image 59
Charles Duffy Avatar answered Nov 03 '22 07:11

Charles Duffy


This part is there for the script to work on Solaris 10 and older where /bin/sh is not POSIX compliant. Note that the latter is not a bug as POSIX doesn't specify what sh path should be.

/bin/sh (and /sbin/sh) on Solaris 10 are probably the only remaining shells in current OSes still supporting this form of pipe which appeared in the original Bourne shell.

like image 41
jlliagre Avatar answered Nov 03 '22 07:11

jlliagre