From 60dadfc9b936c684bbe46346768b771a3f1dc62f Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Wed, 7 Oct 2020 08:03:06 -0400 Subject: [PATCH] Start of check_user functions --- documents/references.md | 12 +++++++- utils.sh | 67 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) mode change 100644 => 100755 utils.sh diff --git a/documents/references.md b/documents/references.md index c69740a..7e73648 100644 --- a/documents/references.md +++ b/documents/references.md @@ -27,4 +27,14 @@ Various references used in project. ## References -None so far. +### Including other Script Files +<https://stackoverflow.com/a/10823650> + +### Check Number of Passed Args +<https://stackoverflow.com/a/6482403> + +### Get User's name +<https://stackoverflow.com/a/19306837> + +### String Manipulation +<https://stackoverflow.com/a/14703709> diff --git a/utils.sh b/utils.sh old mode 100644 new mode 100755 index f1f641a..cf3cb1b --- a/utils.sh +++ b/utils.sh @@ -1 +1,68 @@ #!/usr/bin/env bash +### + # A helper/utility script to be imported by other scripts. + # + # Intended to hold very common functionality (that's surprisingly not built into bash) such as string upper/lower, + # and checking current user value, and prompts of yes/no user input. + ## + + +### + # Function to check if current username matches passed value. + # In particular, is used to check if user is sudo/root user. + ## +function check_is_user () { + + # Check number of passed function args. Continue. + if [[ $# == 1 ]] + then + + local username=$USER + local check_user=$1 + + echo "user is ${username}." + echo "check_user is ${check_user}" + + # Handle for too many args. + elif [[ $# > 1 ]] + then + echo "Too many args passed. Expected one arg of username to check against." + exit 1 + + # Handle for too few args. + else + echo "Too few args passed. Expected one arg of username to check against." + exit 1 + fi +} + + +### + # Function to check if current username does not match passed value. + # In particular, is used to check if user is sudo/root user. + ## +function check_is_not_user () { + + # Check number of passed function args. + if [[ $# == 1 ]] + then + + # Got the expected number of function args. Continue. + local username=$USER + local check_user=$1 + + echo "user is ${username}." + echo "check_user is ${check_user}" + + # Handle for too many args. + elif [[ $# > 1 ]] + then + echo "Too many args passed. Expected one arg of username to check against." + exit 1 + + # Handle for too few args. + else + echo "Too few args passed. Expected one arg of username to check against." + exit 1 + fi +} -- GitLab