Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fix broken python code that makes assumptions about /usr/bin/python.
#!/bin/sh
# Fix all shebangs of files in a directory which do not explicitly state the
# major version number of the Python interpreter, ie refer to /usr/bin/python
# and assume that it is 2.x (or 3.x, but most Python 3 projects already use
# /usr/bin/env python3).
echo "Explicit is better than implicit!
This will fix all shebangs referring to /usr/bin/python
in all files below the current directory.
THIS OPERATION IS RECURSIVE AND DESTRUCTIVE! USE A VCS!
"
read -p "Proceed? (y/n) " answer
case $answer in
[yY])
echo -n "Fixing shebangs..."
grep -Rl '#!/usr/bin/python$' |\
xargs sed -i 's#\(/usr/bin/\)python#\1env python2#'
echo "done"
exit 0
;;
*)
echo "Aborting."
exit 1
;;
esac
@proxypoke

This comment has been minimized.

Copy link
Owner Author

commented Aug 26, 2014

(Consider this public domain, I don't give a shit.)

@tabascoeye

This comment has been minimized.

Copy link

commented Aug 27, 2014

don't you mean grep -Rl '#!/usr/bin/python$' * |\ xargs sed -i 's#\(/usr/bin/\)python#\1env python2#' ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.