mirror of
https://github.com/natelandau/shell-scripting-templates.git
synced 2025-11-14 16:03:47 -05:00
New script template
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
|
||||
|
||||
##################################################
|
||||
# Parse commandline options
|
||||
#
|
||||
# Version 1.0.0
|
||||
#
|
||||
# Works in along with the 'Command Line Options' and 'Set Switches' functions
|
||||
# of many of my scripts
|
||||
#
|
||||
|
||||
@@ -3,8 +3,11 @@
|
||||
# ##################################################
|
||||
# Shared bash functions used by my mac setup scripts.
|
||||
#
|
||||
# VERSION 1.0.0
|
||||
#
|
||||
# HISTORY
|
||||
# * 2015-01-02 - Initial creation
|
||||
#
|
||||
# * 2015-01-02 - v1.0.0 - First Creation
|
||||
#
|
||||
# ##################################################
|
||||
|
||||
@@ -17,33 +20,29 @@
|
||||
hasHomebrew () {
|
||||
# Check for Homebrew
|
||||
if type_not_exists 'brew'; then
|
||||
e_error "No Homebrew. Gots to install it."
|
||||
warning "No Homebrew. Gots to install it."
|
||||
seek_confirmation "Install Homebrew?"
|
||||
if is_confirmed; then
|
||||
# Ensure that we can actually, like, compile anything.
|
||||
if [[ ! "$(type -P gcc)" && "$OSTYPE" =~ ^darwin ]]; then
|
||||
e_error "XCode or the Command Line Tools for XCode must be installed first."
|
||||
notice "XCode or the Command Line Tools for XCode must be installed first."
|
||||
seek_confirmation "Install Command Line Tools from here?"
|
||||
if is_confirmed; then
|
||||
xcode-select --install
|
||||
else
|
||||
e_error "Please come back after Command Line Tools are installed. Exiting"
|
||||
exit 1
|
||||
die "Please come back after Command Line Tools are installed."
|
||||
fi
|
||||
fi
|
||||
# Check for Git
|
||||
if type_not_exists 'git'; then
|
||||
e_error "Git should be installed. It isn't. Aborting."
|
||||
exit 1
|
||||
die "Git should be installed. It isn't. Aborting."
|
||||
fi
|
||||
# Install Homebrew
|
||||
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
|
||||
brew tap homebrew/dupes
|
||||
brew tap homebrew/versions
|
||||
else
|
||||
e_error "Without Homebrew installed we won't get very far."
|
||||
e_error "Exiting"
|
||||
exit 0
|
||||
die "Without Homebrew installed we won't get very far."
|
||||
fi
|
||||
fi
|
||||
}
|
||||
@@ -141,7 +140,7 @@ function doInstall () {
|
||||
done
|
||||
fi
|
||||
else
|
||||
e_success "Nothing to install. You've already got them all."
|
||||
success "Nothing to install. You've already got them all."
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -155,18 +154,18 @@ brewCleanup () {
|
||||
|
||||
# htop
|
||||
if [[ "$(type -P $binroot/htop)" && "$(stat -L -f "%Su:%Sg" "$binroot/htop")" != "root:wheel" || ! "$(($(stat -L -f "%DMp" "$binroot/htop") & 4))" ]]; then
|
||||
e_header "Updating htop permissions"
|
||||
header "Updating htop permissions"
|
||||
sudo chown root:wheel "$binroot/htop"
|
||||
sudo chmod u+s "$binroot/htop"
|
||||
fi
|
||||
if [[ "$(type -P $binroot/bash)" && "$(cat /etc/shells | grep -q "$binroot/bash")" ]]; then
|
||||
e_header "Adding $binroot/bash to the list of acceptable shells"
|
||||
header "Adding $binroot/bash to the list of acceptable shells"
|
||||
echo "$binroot/bash" | sudo tee -a /etc/shells >/dev/null
|
||||
fi
|
||||
if [[ "$SHELL" != "$binroot/bash" ]]; then
|
||||
e_header "Making $binroot/bash your default shell"
|
||||
header "Making $binroot/bash your default shell"
|
||||
sudo chsh -s "$binroot/bash" "$USER" >/dev/null 2>&1
|
||||
e_success "Please exit and restart all your shells."
|
||||
success "Please exit and restart all your shells."
|
||||
fi
|
||||
brew cleanup
|
||||
}
|
||||
@@ -178,9 +177,9 @@ brewCleanup () {
|
||||
# ------------------------------------------------------
|
||||
hasDropbox () {
|
||||
# Confirm we have Dropbox installed
|
||||
e_arrow "Confirming that Dropbox is installed..."
|
||||
notice "Confirming that Dropbox is installed..."
|
||||
if [ ! -e /Applications/Dropbox.app ]; then
|
||||
e_error "We don't have Dropbox. Let's get it installed."
|
||||
notice "We don't have Dropbox. Let's get it installed."
|
||||
seek_confirmation "Install Dropbox and all necessary prerequisites?"
|
||||
if is_confirmed; then
|
||||
# Run functions
|
||||
@@ -196,10 +195,9 @@ hasDropbox () {
|
||||
Install
|
||||
open -a dropbox
|
||||
else
|
||||
e_error "Can't run this script. Install Dropbox manually. Exiting."
|
||||
exit 0
|
||||
die "Can't run this script. Install Dropbox manually."
|
||||
fi
|
||||
else
|
||||
e_success "Dropbox is installed."
|
||||
success "Dropbox is installed."
|
||||
fi
|
||||
}
|
||||
@@ -3,20 +3,30 @@
|
||||
# ##################################################
|
||||
# Shared bash functions used by my bash scripts.
|
||||
#
|
||||
# VERSION 1.0.0
|
||||
#
|
||||
# HISTORY
|
||||
# * 2015-01-02 - Initial creation
|
||||
#
|
||||
# * 2015-01-02 - v1.0.0 - First Creation
|
||||
#
|
||||
# ##################################################
|
||||
|
||||
# scriptPath
|
||||
# Traps
|
||||
# ------------------------------------------------------
|
||||
# This function will populate the variable SOURCEPATH with the
|
||||
# full path of the script being run.
|
||||
# Note: The function must be run within the script before using
|
||||
# the variable
|
||||
# These functions are for use with different trap scenarios
|
||||
# ------------------------------------------------------
|
||||
function scriptPath() {
|
||||
SCRIPTPATH=$( cd "$( dirname "$0" )" && pwd )
|
||||
|
||||
# Non destructive exit for when script exits naturally.
|
||||
# Usage: Add this function at the end of every script
|
||||
function safeExit() {
|
||||
# Delete temp files, if any
|
||||
if is_not_empty "${tmpDir}"; then
|
||||
if is_dir "${tmpDir}"; then
|
||||
rm -r "${tmpDir}"
|
||||
fi
|
||||
fi
|
||||
trap - INT TERM EXIT
|
||||
exit
|
||||
}
|
||||
|
||||
# readFile
|
||||
@@ -34,6 +44,10 @@ function readFile() {
|
||||
done < "$1"
|
||||
}
|
||||
|
||||
# Escape a string
|
||||
# ------------------------------------------------------
|
||||
escape() { echo $@ | sed 's/\//\\\//g'; }
|
||||
|
||||
# needSudo
|
||||
# ------------------------------------------------------
|
||||
# If a script needs sudo access, call this function which
|
||||
@@ -45,16 +59,6 @@ function needSudo() {
|
||||
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &
|
||||
}
|
||||
|
||||
# die
|
||||
# ------------------------------------------------------
|
||||
# "die function" - used to denote a failed action in a script.
|
||||
# usage: cd some/path || die "cd failed"
|
||||
# ------------------------------------------------------
|
||||
die() {
|
||||
e_error "FATAL ERROR: $* (status $?)" 1>&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
# convertsecs
|
||||
# ------------------------------------------------------
|
||||
# Convert Seconds to human readable time
|
||||
@@ -86,7 +90,7 @@ function convertsecs() {
|
||||
function pushover() {
|
||||
# Check for config file containing API Keys
|
||||
if [ ! -f "../etc/pushover.cfg" ]; then
|
||||
e_error "Please locate the pushover.cfg to send notifications to Pushover."
|
||||
error "Please locate the pushover.cfg to send notifications to Pushover."
|
||||
else
|
||||
# Grab variables from the config file
|
||||
source "../etc/pushover.cfg"
|
||||
@@ -243,22 +247,16 @@ function is_os() {
|
||||
|
||||
# Ask the question
|
||||
function seek_confirmation() {
|
||||
force=0
|
||||
echo ""
|
||||
e_bold "$@"
|
||||
read -p " (y/n) " -n 1
|
||||
echo ""
|
||||
}
|
||||
|
||||
# same as above but underlined
|
||||
function seek_confirmation_head() {
|
||||
echo ""
|
||||
e_underline "$@"
|
||||
input "$@"
|
||||
read -p " (y/n) " -n 1
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Test whether the result of an 'ask' is a confirmation
|
||||
function is_confirmed() {
|
||||
(($force)) && return 0;
|
||||
if [[ "$REPLY" =~ ^[Yy]$ ]]; then
|
||||
return 0
|
||||
fi
|
||||
@@ -310,7 +308,7 @@ function unmountDrive() {
|
||||
|
||||
function help () {
|
||||
echo "" 1>&2
|
||||
e_bold " ${@}" 1>&2
|
||||
input " ${@}" 1>&2
|
||||
if [ -n "${usage}" ]; then # print usage information if available
|
||||
echo " ${usage}" 1>&2
|
||||
fi
|
||||
|
||||
@@ -3,8 +3,11 @@
|
||||
# ##################################################
|
||||
# Shared bash functions used by my bash scripts.
|
||||
#
|
||||
# VERSION 1.0.0
|
||||
#
|
||||
# HISTORY
|
||||
# * 2015-01-02 - Initial creation
|
||||
#
|
||||
# * 2015-01-02 - v1.0.0 - First Creation
|
||||
#
|
||||
# ##################################################
|
||||
|
||||
@@ -12,18 +15,19 @@
|
||||
# ------------------------------------------------------
|
||||
# Will return the name of the script being run
|
||||
# ------------------------------------------------------
|
||||
SCRIPTNAME=`basename $0` #Set Script Name variable
|
||||
scriptName=`basename $0` #Set Script Name variable
|
||||
scriptBasename="$(basename ${scriptName} .sh)" # Strips '.sh' from scriptName
|
||||
|
||||
# NOW
|
||||
# ------------------------------------------------------
|
||||
# Will print the current date and time in the format:
|
||||
# 01-02-2015 01:09:54 PM
|
||||
# ------------------------------------------------------
|
||||
NOW=$(date +"%m-%d-%Y %r") #Set Timestamp in variable
|
||||
now=$(date +"%m-%d-%Y %r") #Set Timestamp in variable
|
||||
|
||||
# THISHOST
|
||||
# ------------------------------------------------------
|
||||
# Will print the current hostname of the computer the script
|
||||
# is being run on.
|
||||
# ------------------------------------------------------
|
||||
THISHOST=$(hostname)
|
||||
thisHost=$(hostname)
|
||||
94
lib/utils.sh
94
lib/utils.sh
@@ -1,5 +1,20 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# ##################################################
|
||||
# Bash scripting Utilities.
|
||||
#
|
||||
# VERSION 1.0.0
|
||||
#
|
||||
# This script sources my collection of scripting utilities making
|
||||
# it possible to source this one script and gain access to a
|
||||
# complete collection of functions, variables, and other options.
|
||||
#
|
||||
# HISTORY
|
||||
#
|
||||
# * 2015-01-02 - v1.0.0 - First Creation
|
||||
#
|
||||
# ##################################################
|
||||
|
||||
|
||||
# Source additional files
|
||||
# ------------------------------------------------------
|
||||
@@ -26,18 +41,19 @@ FILES="
|
||||
# Source the Utility Files
|
||||
for file in $FILES
|
||||
do
|
||||
if [ -f "${SOURCE}PATH/${file}" ]; then
|
||||
source "${SOURCE}PATH/${file}"
|
||||
if [ -f "${SOURCEPATH}/${file}" ]; then
|
||||
source "${SOURCEPATH}/${file}"
|
||||
else
|
||||
e_error "${file} does not exist. Exiting"
|
||||
error "${file} does not exist. Exiting"
|
||||
Exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
|
||||
# Logging and Colors
|
||||
# ------------------------------------------------------
|
||||
# Here we set the colors for our script feedback.
|
||||
# Example usage: e_success "sometext"
|
||||
# Example usage: success "sometext"
|
||||
#------------------------------------------------------
|
||||
|
||||
# Set Colors
|
||||
@@ -50,20 +66,60 @@ green=$(tput setaf 76)
|
||||
tan=$(tput setaf 3)
|
||||
blue=$(tput setaf 38)
|
||||
|
||||
# Headers and Logging
|
||||
e_header() { echo -e "\n${bold}${purple}========== $@ ==========${reset}\n" ; }
|
||||
e_arrow() { echo -e "➜ $@" ; }
|
||||
e_success() { echo -e "${green}✔ $@${reset}" ; }
|
||||
e_error() { echo -e "${red}✖ $@${reset}" ; }
|
||||
e_warning() { echo -e "${tan}➜ $@${reset}" ; }
|
||||
e_underline() { echo -e "${underline}${bold}$@${reset}" ; }
|
||||
e_bold() { echo -e "${bold}$@${reset}" ; }
|
||||
e_note() { echo -e "${underline}${bold}${blue}Note:${reset} ${blue}$@${reset}" ; }
|
||||
function _alert() { #my function
|
||||
if [ "${1}" = "emergency" ]; then
|
||||
local color="${bold}${red}"
|
||||
fi
|
||||
if [ "${1}" = "error" ] || [ "${1}" = "warning" ]; then
|
||||
local color="${red}"
|
||||
fi
|
||||
if [ "${1}" = "success" ]; then
|
||||
local color="${green}"
|
||||
fi
|
||||
if [ "${1}" = "debug" ]; then
|
||||
local color="${purple}"
|
||||
fi
|
||||
if [ "${1}" = "header" ]; then
|
||||
local color="${bold}""${purple}"
|
||||
fi
|
||||
if [ "${1}" = "input" ]; then
|
||||
local color="${bold}"
|
||||
_printLog="0" # Don't print to $logFile
|
||||
fi
|
||||
if [ "${1}" = "info" ] || [ "${1}" = "notice" ]; then
|
||||
local color="" # Us terminal default color
|
||||
fi
|
||||
# Don't use colors on pipes or non-recognized terminals
|
||||
if [[ "${TERM}" != "xterm"* ]] || [ -t 1 ]; then
|
||||
color=""; reset=""
|
||||
fi
|
||||
|
||||
# Print to $logFile
|
||||
if [[ "${printLog}" = "1" ]]; then
|
||||
echo -e "$(date +"%m-%d-%Y %r") $(printf "[%9s]" ${1}) "${_message}"" >> $logFile;
|
||||
fi
|
||||
|
||||
# Print to console when script is not 'quiet'
|
||||
((quiet)) && return || echo -e "$(date +"%r") ${color}$(printf "[%9s]" ${1}) "${_message}"${reset}";
|
||||
|
||||
}
|
||||
|
||||
function die () { local _message="${@} Exiting."; echo "$(_alert emergency)"; exit 1;}
|
||||
function error () { local _message="${@}"; echo "$(_alert error)"; }
|
||||
function warning () { local _message="${@}"; echo "$(_alert warning)"; }
|
||||
function notice () { local _message="${@}"; echo "$(_alert notice)"; }
|
||||
function info () { local _message="${@}"; echo "$(_alert info)"; }
|
||||
function debug () { local _message="${@}"; echo "$(_alert debug)"; }
|
||||
function success () { local _message="${@}"; echo "$(_alert success)"; }
|
||||
function input() { local _message="${@}"; echo "$(_alert input)"; }
|
||||
function header() { local _message="========== ${@} ========== "; echo "$(_alert header)"; }
|
||||
|
||||
# Log messages when verbose is set to "1"
|
||||
verbose() { (($verbose)) && info "$@"; }
|
||||
|
||||
|
||||
|
||||
|
||||
# Note to self
|
||||
# Notes to self
|
||||
# ####################
|
||||
# This is how you create a variable with multiple lines
|
||||
# read -d '' String <<"EOF"
|
||||
# one
|
||||
@@ -72,3 +128,9 @@ e_note() { echo -e "${underline}${bold}${blue}Note:${reset} ${blue}$@${reset}"
|
||||
# four
|
||||
# EOF
|
||||
# echo ${String}
|
||||
#
|
||||
# # How to get a script name
|
||||
# scriptLocation="${0}"
|
||||
# scriptFile="${scriptLocation##*/}"
|
||||
# scriptName="${scriptFile%.*}"
|
||||
# echo "${scriptName}"
|
||||
|
||||
Reference in New Issue
Block a user