Skip to content

Instantly share code, notes, and snippets.

@nickhutchinson
Last active January 1, 2020 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickhutchinson/22dc5fbcc722d6b604d422e3b7efc9ae to your computer and use it in GitHub Desktop.
Save nickhutchinson/22dc5fbcc722d6b604d422e3b7efc9ae to your computer and use it in GitHub Desktop.
#!/bin/bash
set -euo pipefail
print_usage() {
>&2 echo "USAGE: $0 [-2 | -2.x | -3 | -3.x] [script]"
}
PY_PYTHON=${PY_PYTHON:-2}
interpreter=()
while [[ $# -gt 0 ]]; do
case "$1" in
--)
shift
break
;;
-2 | -2.?)
interpreter=("python${1#-}")
shift
;;
-3 | -3.?)
interpreter=("python${1#-}")
shift
;;
-h | --help)
print_usage
exit 0
;;
-*)
>&2 echo "$0: Unrecognised argument: $1"
print_usage
exit 1
;;
*)
break
;;
esac
done
if [[ -z "${interpreter[*]:-}" ]] && [[ $# -gt 0 ]] && [[ -f "$1" ]]; then
# Has shebang?
read -r shebang < "$1"
case "$shebang" in
\#!*)
read -r -a interpreter <<< "${shebang#\#!}"
;;
esac
fi
if [[ -z "${interpreter[*]:-}" ]]; then
interpreter=("python${PY_PYTHON}")
fi
if [[ "${VERBOSE:-0}" != "0" ]]; then
>&2 echo "+ ${interpreter[*]} $*"
fi
exec "${interpreter[@]}" "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment