Skip to content

Instantly share code, notes, and snippets.

@Tony3-sec
Last active November 5, 2015 08:56
Show Gist options
  • Save Tony3-sec/a65b8b33a406a200f627 to your computer and use it in GitHub Desktop.
Save Tony3-sec/a65b8b33a406a200f627 to your computer and use it in GitHub Desktop.
Add shenbang to scripts according to file extension. Revised to work in Mac OS-X.
#!/bin/bash
## This script will read the file extension and add proper shenbang to the script
# Exit if no args
if [ $# -eq 0 ]; then
echo "Usage: $0 [filename]"
exit 1
fi
for file in $@
do
# Exit if file does not exist
if [ ! -f $file ]; then
echo "file does not exist"
exit 2
fi
# Get file extension
ext=$(echo "$file" | rev | cut -c 1-3 | rev)
# Delete old shenbang and add new one. Exit if file type is unknown
case "$ext" in
\.sh)
sed -i '' -e '/^#!/d' $file; sed -i '' -e '1i\
\#!/bin/bash
' $file
;;
\.py)
sed -i '' -e '/^#!/d' $file; sed -i '' -e '1i\
\#!/usr/bin/python
' $file
;;
\.rb)
sed -i '' -e '/^#!/d' $file; sed -i '' -e '1i\
\#!/usr/bin/ruby
' $file
;;
\.pl)
sed -i '' -e '/^#!/d' $file; sed -i '' -e '1i\
\#!/usr/bin/perl
' $file
;;
*)
echo "Unknown file type: $file"
exit 3
esac
echo "$file: complete!"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment