#!/bin/bash # touchx # Description: creates a new shell script in ~/Applications/UNIX/, makes it executable (chmod a+x), inserts some standard boilerplate, and opens it for editing ### Requires one argument: the name of the new script # Error codes ARGS=1 && ERR_ARGS=65 AUTHOR="Scott Russell, IT Support Engineer, University of Notre Dame" if [[ $# != "$ARGS" ]]; then echo echo " Usage: `basename $0` [Author]" echo echo " Description: `basename $0` will create .sh in \$HOME/Applications/UNIX/," echo " chmod filename.sh to a+x, insert some standard boilerplate text," echo " and open filename.sh for editing in BBEdit, TextEdit, or vi." echo echo " The [Author] parameter is optional. If not specified, it will default to" echo " \"$AUTHOR\"" echo " (The default Author can be altered by editing this script.)" echo exit $ERR_ARGS fi ### Set a global variable for the application path and create the directory, if necessary APP_PATH="$HOME/Applications/UNIX" if [[ ! -d "${APP_PATH}" ]]; then mkdir "${APP_PATH}" fi ### Set a global variable for the script file we're creating SCRIPT="${APP_PATH}/${1}.sh" ### Simple script writing function script_write() { echo "${1}" >> "${SCRIPT}" } ### Does the script file already exist? if [[ -f "${SCRIPT}" ]]; then EXIST=true else EXIST=false touch "${SCRIPT}" fi ### Make the file executable chmod a+x "${SCRIPT}" ### Echo some basic content into the file ### If the file already exists, leave it alone if [[ "$EXIST" == false ]]; then script_write "#!/bin/bash" script_write "# ${1}.sh" script_write "# Description: " script_write "" script_write "# Written by: $AUTHOR" script_write "# Created on: $(date)" script_write "# Last Modified: $(date)" script_write "" script_write "" script_write "exit 0" fi ### Open the script file in BBEdit or TextWrangler or TextEdit or vi bbedit "${SCRIPT}" 2>/dev/null || edit "${SCRIPT}" 2>/dev/null || open -e "${SCRIPT}" 2>/dev/null || vi "${SCRIPT}" exit 0