Skip to content

Instantly share code, notes, and snippets.

@mildmelon mildmelon/mkscript.sh

Last active Dec 7, 2019
Embed
What would you like to do?
This script will create a header for a new bash script providing default values, and open the created script in Vim, NeoVim, or Emacs.
#!/bin/bash
# title : mkscript.sh
# description : This script will make a header for a bash script.
# author : Caidan Williams https://github.com/mildmelon
# date : 2019-06-25
# version : 1.4.7
# usage : ./mkscript.sh
# notes : Install Vim or NeoVim to open the created script.
# bash_version : 5.0.3(1)-release
# ======================================================================================================================
today=$(date +%F)
div_char="="
default_width=120
default_title="new_script"
default_name="${USER}"
default_version="1.0.0"
function select_width() {
# Get the user input
printf "Enter script width [${default_width}]: "; read -r width
# Set width to if no input is given
[[ ${#width} == 0 ]] && width=${default_width}
# Check that width is a number
re="^[0-9]+$"
if ! [[ ${width} =~ $re ]]; then
printf "error: %s\n%s\n\n" \
"\"${width}\" is not a valid number." \
"Please enter another number." >&2
select_width
else
# Subtract 2 from width to accommodate "# " characters
width=$(expr ${width} - 2)
# Format the divider
div=$(printf "${div_char}%.0s" $(eval echo {1..${width}}))
fi
}
select_width
function should_overwrite() {
# Prompt for overwriting existing file
printf "Overwrite? [y/N] "; read -r -n 1 overwrite
# Convert lowercase to uppercase
overwrite=${overwrite^^}
if [[ ${overwrite} == "Y" ]]; then
overwrite=true
else
overwrite=false
fi
}
function select_title() {
# Get the user input
printf "Enter title [${default_title}]: "; read -r title
# Set title if no input is given
[[ ${#title} == 0 ]] && title=${default_title}
# Remove the spaces from the title if necessary
title=${title// /_}
# Convert uppercase to lowercase
title=${title,,}
# Add .sh to the end of the title if it is not there already
[[ "${title: -3}" != ".sh" ]] && title="${title}.sh"
# Check to see if the file exists already.
if [[ -e ${title} ]]; then
printf "Script \"${title}\" already exists.\n"
should_overwrite
if [[ ${overwrite} == false ]]; then
printf "Please select another title.\n\n"
select_title
else
# Add a new line to separate overwrite input and description input
printf "\n"
fi
fi
}
select_title
printf "Enter description: "; read -r description
printf "Enter author name [${default_name}]: "; read -r name;
[[ ${#name} == 0 ]] && default_name=${default_name}
printf "Enter version number [${default_version}]: "; read -r version_number
[[ ${#version_number} == 0 ]] && version_number=${default_version}
# Format the output and write it to a file
printf "%-16s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%s\n\n\n" \
"#!/bin/bash" \
"# title" ": ${title}" \
"# description" ": ${description}" \
"# author" ": ${name}" \
"# date" ": ${today}" \
"# version" ": ${version_number}" \
"# usage" ": ./${title}" \
"# notes" ": " \
"# bash_version" ": ${BASH_VERSION}" \
"# ${div}" > ${title}
# Make the file executable
chmod +x ${title}
function select_editor() {
# Select between Vim, NeoVim, or Emacs
printf "\n%s\n%s\n%s\n%s\n" \
"Select an editor:" \
" 1: Vim" \
" 2: NeoVim" \
" 3: Emacs"
read -r editor
# Open the file with the cursor on line 12
case ${editor} in
1) vim +12 ${title}
;;
2) nvim +12 ${title}
;;
3) emacs +12 ${title} &
;;
*) /usr/bin/clear
printf "%s\n%s\n\n" \
"I did not understand your selection." \
"Press <Ctrl-c> to quit." >&2
select_editor
;;
esac
}
select_editor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.