Skip to content

Instantly share code, notes, and snippets.

@proxypoke
Created August 26, 2014 12:18
Show Gist options
  • Save proxypoke/d32a99483533d1ca664d to your computer and use it in GitHub Desktop.
Save proxypoke/d32a99483533d1ca664d to your computer and use it in GitHub Desktop.
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
Copy link
Author

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

@tabascoeye
Copy link

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