Created
March 25, 2016 16:25
-
-
Save Node0/6c01e30473fbd382f705 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function userList () { | |
# Help string | |
helpText="Usage: basename [-h]|[--help] [--username] [--homedir] [--uid] [--gid] [--ugid] | |
----------------------------------------------------------------; | |
Help: -h or --help, shows this help page. | |
username: --username, fetches a list of usernames which are associated with a current user account. | |
homedir: --homedir, fetches a space delimited list of usernames to home directores. | |
uid: --uid, fetches a space delimited list of usernames to user ids. | |
gid: --gid, fetches a space delimited list of usernames to group ids. | |
ugid: --ugid, fetches a space delimited list of usernames to user ids and group ids. | |
----------------------------------------------------------------\n\n"; | |
if [[ $( echo "${#} < 1" | bc ) != 0 ]]; then | |
echo -ne "${helpText}"; | |
return | |
fi | |
# Parameter Regexes | |
# Note: Regexes structured with regard for parameter-case | |
# in order to ensure smooth interchangability between grep and sed | |
# without regard to version (of sed) or system. Per-tool case | |
# sensitivity flags may have otherwise resulted in more brittle code. | |
helpShort="^\-[Hh]$"; | |
helpLong="^\-\-[Hh][Ee][Ll][Pp]$"; | |
userName="^\-\-[Uu][Ss][Ee][Rr][Nn][Aa][Mm][Ee]$" | |
homeDir="^\-\-[Hh][Oo][Mm][Ee][Dd][Ii][Rr]$" | |
uid="^\-\-[Uu][Ii][Dd]$" | |
gid="^\-\-[Gg][Ii][Dd]$" | |
ugid="^\-\-[Uu][Gg][Ii][Dd]$" | |
# Out of order parameter processing loop (script may be invoked with params given in any order). | |
paramList=("${@}"); | |
for param in "${paramList[@]}"; | |
do | |
# In general: Detect presence of parameter i.e. ( $(echo ${param} | grep -Po) != "" ) | |
# then handle the particulars using sed for access if the param is a key:value pair. | |
# Handle help flag detecting and display usage info / help message. | |
if [[ "$(echo "${param}" | \grep -Po '('${helpShort}')' )" != "" ]]; then | |
echo -ne "${helpText}"; | |
return | |
elif [[ "$(echo "${param}" | \grep -Po '('${helpLong}')' )" != "" ]]; then | |
echo -ne "${helpText}"; | |
return | |
fi | |
# Handle username | |
if [[ "$(echo "${param}" | \grep -Po '('${userName}')' )" != "" ]]; then | |
\awk '{FS=":"};{print $1}' /etc/passwd; | |
#Handle homedir | |
elif [[ "$(echo "${param}" | \grep -Po '('${homeDir}')' )" != "" ]]; then | |
\awk '{FS=":"};{print $1" "$6}' /etc/passwd; | |
# Handle uid | |
elif [[ "$(echo "${param}" | \grep -Po '('${uid}')' )" != "" ]]; then | |
\awk '{FS=":"};{print $1" "$3}' /etc/passwd; | |
# Handle gid | |
elif [[ "$(echo "${param}" | \grep -Po '('${gid}')' )" != "" ]]; then | |
\awk '{FS=":"};{print $1" "$3}' /etc/passwd; | |
# Handle user & group id | |
elif [[ "$(echo "${param}" | \grep -Po '('${ugid}')' )" != "" ]]; then | |
\awk '{FS=":"};{print $1" "$3" "$4}' /etc/passwd; | |
fi | |
done; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment