Skip to content

Instantly share code, notes, and snippets.

@gmlp
Last active December 19, 2018 17:54
Show Gist options
  • Save gmlp/10f6e13226aed7eed78cdc4c25d0678d to your computer and use it in GitHub Desktop.
Save gmlp/10f6e13226aed7eed78cdc4c25d0678d to your computer and use it in GitHub Desktop.
#!/bin/bash
###########################################################################################################################################################################
# Sample #1
#
# Write a program most_active_users that is called with two command line arguments, an /etc/passwd file and its respective /etc/group file, eg:
#
# most_active_users passwd group
# most_active_users should write to stdout the group with the highest number of users that can currently log in, along with the actual count, delimited by a : character.
#
# For example, given then included files passwd and group, the output should be:
#
# root:3
#
###########################################################################################################################################################################
readonly AVAILABLE_SHELLS="/bin/bash"
function print_usage {
echo
echo "Usage: most_active_users [OPTIONS]"
echo
echo "This script can be used to get the highest number of users that can currently login."
echo
echo "Options:"
echo
echo -e " --group-file\t\tfile path for group file."
echo -e " --passwd-file\t\tfile path for passwd file."
echo
echo "Example:"
echo
echo " ./most_active_users --passwd-file passwd --group-file group"
}
function log {
local -r level="$1"
local -r message="$2"
local -r timestamp=$(date +"%Y-%m-%d %H:%M:%S")
>&2 echo -e "${timestamp} [${level}] [$SCRIPT_NAME] ${message}"
}
function log_info {
local -r message="$1"
log "INFO" "$message"
}
function log_warn {
local -r message="$1"
log "WARN" "$message"
}
function log_error {
local -r message="$1"
log "ERROR" "$message"
}
function assert_not_empty {
local -r arg_name="$1"
local -r arg_value="$2"
if [[ -z "$arg_value" ]]; then
log_error "The value for '$arg_name' cannot be empty"
print_usage
exit 1
fi
}
function user_has_shell(){
local -r user_shell="$1"
[[ $user_shell == *"${AVAILABLE_SHELLS}"* ]]
}
function get_group_with_most_active_users {
local top_grp=""
local let top_count=0
local passwd_file=""
local group_file=""
while [[ $# > 0 ]]; do
local key="$1"
case "$key" in
--passwd-file)
passwd_file="$2"
shift
;;
--group-file)
group_file="$2"
shift
;;
--help)
print_usage
exit
;;
*)
log_error "Unrecognized argument: $key"
print_usage
exit 1
;;
esac
shift
done
assert_not_empty "--passwd-file" "$passwd_file"
assert_not_empty "--group-file" "$group_file"
log_info "Starting ..."
for line in `cat ${group_file}`; do
local let tmp_count=0
local tmp_grp=$(echo $line | awk -F ':' '{print $1}')
local users=$(echo $line | awk -F ':' '{print $4}')
for user in `echo ${users} | sed "s/,/ /g"`; do
local user_shell=$(grep "^${user}" $passwd_file | awk -F ':' '{print $7}')
if $(user_has_shell $user_shell ); then
let tmp_count++
fi
done
if (( tmp_count > top_count )); then
top_count=$tmp_count
top_grp=$tmp_grp
fi
done
echo "${top_grp}:${top_count}"
log_info "finishig..."
}
get_group_with_most_active_users "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment