update commands

This commit is contained in:
Nathaniel Landau
2021-10-23 23:00:49 -04:00
parent 46d7edf726
commit 63562be37a
7 changed files with 30 additions and 237 deletions

View File

@@ -115,7 +115,7 @@ _alert_() {
reset=""
fi
printf "%s ${_color}[%7s] %s${reset}\n" "$(date +"%r")" "${_alertType}" "${_message}"
printf "${_color}[%7s] %s${reset}\n" "${_alertType}" "${_message}"
}
_writeToScreen_

View File

@@ -10,6 +10,7 @@ _commandExists_() {
# 1 if false
# USAGE:
# (_commandExists_ ffmpeg ) && [SUCCESS] || [FAILURE]
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
if ! command -v "$1" >/dev/null 2>&1; then
@@ -50,8 +51,6 @@ _isAlpha_() {
# 1 - Input contains non-alphabetic characters
# USAGE:
# _isAlpha_ "${var}"
# NOTES:
#
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _re='^[[:alpha:]]+$'
@@ -71,8 +70,6 @@ _isAlphaNum_() {
# 1 - Input contains alpha-numeric characters
# USAGE:
# _isAlphaNum_ "${var}"
# NOTES:
#
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _re='^[[:alnum:]]+$'
@@ -92,8 +89,6 @@ _isAlphaDash_() {
# 1 - Input is not only alpha-numeric or dash or underscore characters
# USAGE:
# _isAlphaDash_ "${var}"
# NOTES:
#
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _re='^[[:alnum:]_-]+$'
@@ -136,8 +131,6 @@ _isInternetAvailable_() {
# stdout:
# USAGE:
# _isInternetAvailable_
# NOTES:
#
local _checkInternet
if [[ -t 1 || -z ${TERM} ]]; then
@@ -298,7 +291,7 @@ _varIsTrue_() {
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
[[ ${1} == true || ${1} -eq 0 ]] && return 0 || return 1
[[ ${1} == "true" || ${1} == 0 ]] && return 0 || return 1
}
_varIsFalse_() {
@@ -314,7 +307,7 @@ _varIsFalse_() {
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
[[ ${1} == false || ${1} -eq 1 ]] && return 0 || return 1
[[ ${1} == false || ${1} == 1 ]] && return 0 || return 1
}
_varIsEmpty_() {

View File

@@ -232,7 +232,7 @@ _parseDate_() {
trap "$(shopt -p nocasematch)" RETURN # reset nocasematch when function exits
shopt -s nocasematch # Use case-insensitive regex
debug "_parseDate_() input ${tan}$date${purple}"
debug "_parseDate_() input ${tan}${_stringToTest}${purple}"
# YYYY MM DD or YYYY-MM-DD
_pat="(.*[^0-9]|^)((20[0-2][0-9])[-\.\/_ ]+([0-9]{1,2})[-\.\/_ ]+([0-9]{1,2}))([^0-9].*|$)"

View File

@@ -339,7 +339,7 @@ _fileName_() {
# _fileName_ "some/path/to/file.txt" --> "file.txt"
# _fileName_ "some/path/to/file" --> "file"
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
printf "%s" "${1##*/}"
printf "%s\n" "${1##*/}"
}
@@ -404,16 +404,15 @@ _fileExtension_() {
fi
_fn=${_fn%.$_ext}
done
debug "_exts: $_exts"
[[ ${_file} == "${_exts}" ]] && return 1
printf "%s" "${_exts}"
}
_fileDirectory_() {
_filePath_() {
# DESC:
# Finds the directory name from a file path
# Finds the directory name from a file path. If it exists on filesystem, print absolute path. If a string, remove the filename and return the path
# ARGS:
# $1 (Required) - Input string path
# OUTS:
@@ -421,47 +420,24 @@ _fileDirectory_() {
# 1 - Failure
# stdout: Directory path
# USAGE:
# _fileDirectory_ "some/path/to/file.txt" --> "some/path/to"
# _fileDir_ "some/path/to/file.txt" --> "some/path/to"
# CREDIT:
# https://github.com/labbots/bash-utility/
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _tmp=${1:-.}
local _tmp=${1}
[[ ${_tmp} != *[!/]* ]] && { printf '/\n' && return; }
_tmp="${_tmp%%"${_tmp##*[!/]}"}"
[[ ${_tmp} != */* ]] && { printf '.\n' && return; }
_tmp=${_tmp%/*} && _tmp="${_tmp%%"${_tmp##*[!/]}"}"
printf '%s' "${_tmp:-/}"
}
_fileAbsPath_() {
# DESC:
# Gets the absolute path of a file or directory
# ARGS:
# $1 (Required) - Relative path to a file or directory
# OUTS:
# 0 - Success
# 1 - If file/directory does not exist
# stdout: String relative or absolute path to file/directory
# USAGE:
# _fileAbsPath_ "../path/to/file.md" --> /home/user/docs/path/to/file.md
# CREDIT:
# https://github.com/labbots/bash-utility/
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _input="${1}"
if [[ -f ${_input} ]]; then
printf "%s/%s\n" "$(cd "$(_fileDirectory_ "${_input}")" && pwd)" "${_input##*/}"
elif [[ -d ${_input} ]]; then
printf "%s\n" "$(cd "${_input}" && pwd)"
if [ -e "${_tmp}" ]; then
_tmp="$(dirname "$(realpath "${_tmp}")")"
else
return 1
[[ ${_tmp} != *[!/]* ]] && { printf '/\n' && return; }
_tmp="${_tmp%%"${_tmp##*[!/]}"}"
[[ ${_tmp} != */* ]] && { printf '.\n' && return; }
_tmp=${_tmp%/*} && _tmp="${_tmp%%"${_tmp##*[!/]}"}"
fi
printf '%s' "${_tmp:-/}"
}
_fileContains_() {
@@ -523,12 +499,12 @@ _listFiles_() {
[Gg]*)
while read -r _fileMatch; do
printf "%s\n" "$(realpath "${_fileMatch}")"
done < <(find "${_directory}" -iname "${_pattern}" -type f -maxdepth 1 | sort)
done < <(find "${_directory}" -maxdepth 1 -iname "${_pattern}" -type f | sort)
;;
[Rr]*)
while read -r _fileMatch; do
printf "%s\n" "$(realpath "${_fileMatch}")"
done < <(find "${_directory}" -iregex "${_pattern}" -type f -maxdepth 1 | sort)
done < <(find "${_directory}" -maxdepth 1 -iregex "${_pattern}" -type f | sort)
;;
*)
fatal "_listFiles_: Could not determine if search was glob or regex"
@@ -654,93 +630,6 @@ _makeSymlink_() {
return 0
}
_parseFilename_() {
# DESC:
# Break a filename into its component parts which and place them into prefixed
# variables for use in your script. Run with VERBOSE=true to see the variables while
# running your script.
# ARGS:
# $1 (Required) - Path to file to parse. (Must exist in filesystem)
# OPTS:
# -n - optional flag for number of extension levels (Ex: -n2)
# OUTS:
# 0 - Success
# 1 - Error
# Variables created
# $PARSE_FULL - File and its real path (ie, resolve symlinks)
# $PARSE_PATH - Path to the file
# $PARSE_BASE - Name of the file WITH extension
# $PARSE_BASENOEXT - Name of file WITHOUT extension
# $PARSE_EXT - The extension of the file
# USAGE:
# _parseFilename_ "some/file.txt"
# Error handling
if [[ $# -lt 1 ]] \
|| ! command -v dirname &>/dev/null \
|| ! command -v basename &>/dev/null \
|| ! command -v realpath &>/dev/null; then
fatal "Missing dependency or input to ${FUNCNAME[0]}"
fi
local _levels
local option
local _exts
local _ext
local i
local _fn
local OPTIND=1
while getopts ":n:" option; do
case ${option} in
n) _levels=${OPTARG} ;;
*) continue ;;
esac
done && shift $((OPTIND - 1))
local _fileToParse="${1}"
if [ ! -f "${_fileToParse}" ]; then
debug "_parseFile_: Could not find file: ${_fileToParse}"
return 1
fi
PARSE_FULL="$(realpath "${_fileToParse}")" \
&& debug "\${PARSE_FULL}: ${PARSE_FULL:-}"
PARSE_BASE=$(basename "${_fileToParse}") \
&& debug "\${PARSE_BASE}: ${PARSE_BASE}"
PARSE_PATH="$(realpath "$(dirname "${_fileToParse}")")" \
&& debug "\${PARSE_PATH}: ${PARSE_PATH:-}"
# Detect some common multi-extensions
if [[ -z ${_levels:-} ]]; then
case $(tr '[:upper:]' '[:lower:]' <<<"${PARSE_BASE}") in
*.tar.gz | *.tar.bz2 | *.log.[0-9]) _levels=2 ;;
*) _levels=1 ;;
esac
fi
# Find Extension
_fn="${PARSE_BASE}"
for ((i = 0; i < _levels; i++)); do
_ext=${_fn##*.}
if [ $i == 0 ]; then
_exts=${_ext}${_exts:-}
else
_exts=${_ext}.${_exts:-}
fi
_fn=${_fn%.$_ext}
done
if [[ ${_exts} == "${PARSE_BASE}" ]]; then
PARSE_EXT="" && debug "\${PARSE_EXT}: ${PARSE_EXT}"
else
PARSE_EXT="${_exts}" && debug "\${PARSE_EXT}: ${PARSE_EXT}"
fi
PARSE_BASENOEXT="${PARSE_BASE%.$PARSE_EXT}" \
&& debug "\${PARSE_BASENOEXT}: ${PARSE_BASENOEXT}"
}
_parseYAML_() {
# DESC:
# Convert a YAML file into BASH variables for use in a shell script