powdahound (owner)

Revisions

gist: 188362 Download_button fork
public
Description:
http://blog.powdahound.com/post/189982500/mkscript-test-script-creator
Public Clone URL: git://gist.github.com/188362.git
Embed All Files: show embed
mkscript #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/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"