Skip to content

Instantly share code, notes, and snippets.

@powdahound
Created September 17, 2009 05:32
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 powdahound/188362 to your computer and use it in GitHub Desktop.
Save powdahound/188362 to your computer and use it in GitHub Desktop.
#!/bin/bash
###############################################################################
# mkscript is a tool to quickly create executable script files
#
# Instead of:
# echo '#!/usr/bin/python' > test.py
# chmod a+x test.py
# Just do:
# mkscript test.py
#
# If no extension is given, an empty executable file will be created. Supported
# extensions and their initial contents are defined in the $templates variable
# below.
#
# Author: Garret Heaton (http://powdahound.com)
# Created: 2009-09-07
# Last rev: 2009-09-16
###############################################################################
# List of extensions and their initial contents
templates="
php #!/usr/bin/php\n<?php
pl #!/usr/bin/perl -w
py #!/usr/bin/python
rb #!/usr/bin/ruby
sh #!/bin/bash
"
if [ $# -eq 0 ]; then
echo "Usage: mkscript <script.ext>"
exit 1
fi
# create file
file=$1;
if [ -e $file ]; then
echo "'$file' already exists"
exit 2
fi
touch $file
chmod a+x $file
# add shebang if an extension was provided that we have a template for
ext=""
if [[ $file =~ .*\.([a-z]*)$ ]]; then
ext=${BASH_REMATCH[1]}
# find template for this extension
template=`echo "$templates" | grep -E "^$ext"`
if [[ $template =~ ([a-z]*)\ *(.*) ]]; then
echo -e "${BASH_REMATCH[2]}\n\n" > $file
fi
fi
echo "Created script: $file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment