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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With