Last active
January 1, 2020 15:42
-
-
Save nickhutchinson/22dc5fbcc722d6b604d422e3b7efc9ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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