#!/usr/bin/env bash ############# # Variables # ############# # Define formatting variables fb="$(tput bold)" ; readonly fb fr="$(tput sgr0)" ; readonly fr # Define name regular expression name_regex='[^a-zA-Z0-9]' ; readonly name_regex scripts_dir="" ; readonly scripts_dir # Scripts directory # Script Editor script_editor="" ; readonly script_editor # Define menu string menu_str='\n' menu_str+='*************************\n' menu_str+='\n' menu_str+='New Script\n' menu_str+='---------------\n' menu_str+='1) Bash\n' menu_str+='2) Python\n' menu_str+='q) Quit\n' menu_str+='\n' menu_str+='Enter menu selection:' ############# # Functions # ############# display_usage() { local help_msg # Define local variable help_msg="Usage: ${0} Purpose: Create new script. Variables to set: scripts_dir Location of scripts directory. script_editor Program to use for editing files. By default, the script will respect your environment's ${fb}EDITOR${fr} value. If this value is not configured, you can set ${fb}script_editor${fr} to your editor of choice." echo "${help_msg}" } # Sanitize name clean_name() { local cleaned # Replace most non-alphanumeric characters with underscores cleaned="${1//${name_regex}/_}" # Squeeze underscores and lowercase name cleaned="$(echo -n "${cleaned}" | tr -s '_' | tr '[:upper:]' '[:lower:]')" echo "${cleaned}" } # Add front matter to Bash script bash_front_matter() { local header header="#!/usr/bin/env bash # Purpose: ${script_purpose} # Usage: ########### # Aliases # ########### #shopt -s expand_aliases # Enable alias expansion ############# # Variables # ############# ############# # Functions # ############# # Define starting point for execution of the program main() { } ########### # Program # ########### main \"\${@}\" # Start program exit 0 # Exit script with successful exit status" echo "${header}" > "${scripts_dir}/${script_name}.${script_ext}" } # Add front matter to Python script python_front_matter() { local header header="#!/usr/bin/env python3 ''' ${script_purpose} Classes: * Functions: * main() - Define starting point for execution of the program. Requires ---------------- Variables to set: ---------------- Options: ---------------- ''' ########### # Modules # ########### # Enable access to some variables/functions used/maintained by the interpreter import sys ############# # Variables # ############# ########### # Classes # ########### ############# # Functions # ############# def main() -> None: '''Define starting point for execution of the program.''' ########### # Program # ########### if __name__ == '__main__': main() # Start program sys.exit(0) # Exit program with successful exit status" echo "${header}" > "${scripts_dir}/${script_name}.${script_ext}" } # Present menu menu() { local menu_answer # Define local variable echo -e "${menu_str}" # Display menu options # Save menu choice read -r menu_answer # Evaluate menu selection case "${menu_answer}" in '1') script_ext='bash' # Set Bash extension ;; '2') script_ext='py' # Set Python extension ;; 'q') exit 1 ;; *) # Prompt for valid input echo -e "\n${fb}Please enter a valid menu selection.${fr}" 1>&2 menu ;; esac } # Define starting point for execution of the program main() { # Parse arguments parsed_args="$(getopt \ -n "${0##*/}" \ -o h \ --long help \ -- "${@}")" inv_ents="${?}" # Check for invalid entries if [[ "${inv_ents}" -ne 0 ]]; then echo '' display_usage exit 1 fi # Set positional parameters using parsed argument string eval set -- "${parsed_args}" # Evaluate options while true; do case "${1}" in '-h' | '--help') display_usage exit 0 ;; '--') ## End of options shift break ;; esac done # Override EDITOR with script_editor if [[ "${script_editor}" ]]; then EDITOR="${script_editor}" fi # Verify that an editor is set if ! [[ "${EDITOR}" || "${script_editor}" ]]; then # Prompt user to set required variable and exit err_msg="\nSet either ${fb}EDITOR${fr} or ${fb}script_editor${fr} " err_msg+='before running the script again.' echo -e "${err_msg}" 1>&2 exit 1 fi # Verify that required variable is set if ! [[ "${scripts_dir}" ]]; then # Prompt user to set required variable and exit err_msg="\nSet ${fb}scripts_dir${fr} before running the script again." echo -e "${err_msg}" 1>&2 exit 1 fi menu # Call menu function # Prompt for script name, save to variable echo '' read -p 'Enter script name: ' -r script_name script_name="$(clean_name "${script_name}")" # Sanitize input # Prompt for script purpose, save to variable read -p 'Enter script purpose: ' -r script_purpose # Call appropriate front matter function based on script extension if [[ "${script_ext}" == 'bash' ]]; then bash_front_matter elif [[ "${script_ext}" == 'py' ]]; then python_front_matter fi # Make script executable chmod 700 "${scripts_dir}/${script_name}.${script_ext}" # Open script in editor "${EDITOR}" "${scripts_dir}/${script_name}.${script_ext}" } ########### # Program # ########### main "${@}" # Start program exit 0 # Exit script with successful exit status