diff --git a/documents/references.md b/documents/references.md index 6f1f22df57d7fe79b031754906bc53cde8fe2a34..bc9c8816728390e3176c06024c9634fa37ea7ab7 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 3da34e9f87713619b962d7360e87e0a6257203cc..5dd16f74a063d67c83f3229cabf3dcd9a515e104 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.