diff --git a/documents/references.md b/documents/references.md index c69740aa5a357ec3f1c65bdf51152c1cbf888dde..7e7364877c69f7e8a947f57564fb4d1386266ca0 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 f1f641af19bf623505554b29f35499b5d15f3fd3..cf3cb1bb957ae3663f0496c33eef9b4a18341e9f --- 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 +}