From 961b7864ad3ae8c7a81870d1dae5defb922de597 Mon Sep 17 00:00:00 2001 From: "chawley@hq.overdrive.com (chawley-vm)" Date: Tue, 2 Feb 2021 15:46:06 -0500 Subject: [PATCH 1/2] Changes to shell template --- .vim/templates/sh | 17 ++++++++++++++--- .vimrc | 6 +++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/.vim/templates/sh b/.vim/templates/sh index 0bb8ae1..ff9a762 100644 --- a/.vim/templates/sh +++ b/.vim/templates/sh @@ -18,9 +18,20 @@ set -o nounset # Treat unset variables as an error # Check for empty argument if [ -z "${1:-}" ]; then - arg="undefined" + arg="undefined" + echo "Required Argument Missing" + #exit 1 else - arg=$1 + arg=$1 fi -. +# Log Echo: send output to screen and specified file +# Usage: logecho "Log Message" +# define logfile and path to enable logging to file +logfile= +logecho() { + echo "${1}" + if [[ ! -z "${logfile}" ]]; then + echo "${1}" >> "${logfile}" + fi +} diff --git a/.vimrc b/.vimrc index 547765c..4a69bad 100644 --- a/.vimrc +++ b/.vimrc @@ -236,11 +236,11 @@ nnoremap ,begend :-1read $HOME/.vim/templates/begendjA " ==> Templates: shell scripts (.sh) autocmd BufNewFile *.sh so $HOME/.vim/templates/sh -autocmd BufNewFile *.sh %s/FILE:.*/\='FILE: '.expand('%')/e +autocmd BufNewFile *.sh %s/FILE:.*/\='FILE: '.expand('%:t')/e autocmd BufNewFile *.sh exe "g/AUTHOR:.*/s//AUTHOR: C Hawley" -autocmd BufNewFile *.sh exe "g/CREATED:.*/s//CREATED: " .strftime("%c") +autocmd BufNewFile *.sh exe "g/CREATED:.*/s//CREATED: " .strftime("%Y-%m-%d") autocmd BufWritePre,filewritepre *.sh execute "normal ma" -autocmd BufWritePre,filewritepre *.sh exe "g/REVISION:.*/s//REVISION: " .strftime("%c") +autocmd BufWritePre,filewritepre *.sh exe "g/REVISION:.*/s//REVISION: " .strftime("%Y-%m-%d") autocmd bufWritePost,filewritepost *.sh execute "normal `a" " --------------------------------------------------------------------------------------------------------------------- From ed8d7d31d9e426fb57e9dd21501de30f6aa9e7a1 Mon Sep 17 00:00:00 2001 From: "chawley@hq.overdrive.com (chawley-vm)" Date: Fri, 5 Feb 2021 16:38:28 -0500 Subject: [PATCH 2/2] Added root fuction to shell script template --- .vim/templates/sh | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/.vim/templates/sh b/.vim/templates/sh index ff9a762..861e200 100644 --- a/.vim/templates/sh +++ b/.vim/templates/sh @@ -16,7 +16,15 @@ set -o nounset # Treat unset variables as an error -# Check for empty argument +# set variables +declare -r TRUE=0 +declare -r FALSE=1 + +################################################################## +# Purpose: Check for empty argument +# Arguments: at least one +# Return: Either error message or exit with an error code 1 +################################################################## if [ -z "${1:-}" ]; then arg="undefined" echo "Required Argument Missing" @@ -25,9 +33,11 @@ else arg=$1 fi -# Log Echo: send output to screen and specified file +################################################################## +# Purpose: Send output to screen and specified file +# Requires: define to `logfile` variable to send out put to logfile # Usage: logecho "Log Message" -# define logfile and path to enable logging to file +################################################################## logfile= logecho() { echo "${1}" @@ -35,3 +45,15 @@ logecho() { echo "${1}" >> "${logfile}" fi } + +################################################################## +# Purpose: Return true if script is executed by the root user +# Arguments: none +# Return: True or False +# Usage: is_root && echo "root" || echo "not root" +################################################################## +function is_root() +{ + [ $(id -u) -eq 0 ] && return $TRUE || return $FALSE +} +