From 64797c2c8a9cb6fe3f19c8b1fbb4dcfc2a71d163 Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Sat, 10 Oct 2020 04:49:01 -0400 Subject: [PATCH] Create string "to upper/lower" functions --- documents/references.md | 16 +++++++----- utils.sh | 58 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 6 deletions(-) diff --git a/documents/references.md b/documents/references.md index 6f1f22d..bc9c881 100644 --- a/documents/references.md +++ b/documents/references.md @@ -5,8 +5,7 @@ Various references used in project. -## General Bash References - +## General Syntax References ### Shebang Line <https://linuxize.com/post/bash-shebang/> @@ -18,6 +17,7 @@ Various references used in project. ### Functions <https://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-8.html> +<https://www.linuxjournal.com/content/return-values-bash-functions> ### Arrays <https://opensource.com/article/18/5/you-dont-know-bash-intro-bash-arrays> @@ -33,11 +33,15 @@ Various references used in project. ### Check Number of Passed Args <https://stackoverflow.com/a/6482403> -### Change Output Color -<https://stackoverflow.com/a/5947802> - ### Get User's name <https://stackoverflow.com/a/19306837> -### String Manipulation +### Text Manipulation +#### Change Output Color +<https://stackoverflow.com/a/5947802> + +#### String Upper/Lower Case +<https://stackoverflow.com/a/2264537> + +#### String Trimming Manipulation <https://stackoverflow.com/a/14703709> diff --git a/utils.sh b/utils.sh index 3da34e9..5dd16f7 100755 --- a/utils.sh +++ b/utils.sh @@ -21,9 +21,14 @@ text_cyan="\033[1;36m" text_yellow="\033[1;33m" text_white="\033[1;37m" +# Function Return. +return_value="" + #endregion Global Utility Variables +#region User Check Functions + ### # Function to check if current username matches passed value. # In particular, is used to check if user is sudo/root user. @@ -84,6 +89,59 @@ function check_is_not_user () { fi } +#endregion User Check Functions + + +#region Text Manipulation Functions + +### + # Converts one or more passed args to uppercase characters. + ## +function to_upper () { + + # Check number of passed function args. + if [[ $# > 0 ]] + then + # At least one arg passed. Loop through each one. + return_value=() + for arg in $@ + do + return_value+=( $(echo "${arg}" | tr '[:lower:]' '[:upper:]') ) + done + + # No args passed. + else + echo -e "${text_red}Too few args passed. Expected one arg of username to check against.${text_reset}" + exit 1 + fi +} + + +### + # Convers one or more passed args to lowercase characters. + ## +function to_lower () { + + # Check number of passed function args. + if [[ $# > 0 ]] + then + # At least one arg passed. Loop through each one. + return_value=() + for arg in ${@} + do + return_value+=( $(echo "${arg}" | tr '[:upper:]' '[:lower:]') ) + done + + + # No args passed. + else + echo -e "${text_red}Too few args passed. Expected one arg of username to check against.${text_reset}" + exit 1 + fi +} + +#endregion Text Manipulation Functions + ### # Function to print out all available text colors provided by this script. -- GitLab