#!/bin/bash DO_JSON=0 MY_PATH=$(dirname $0) LOGINDEFS="/etc/login.defs" PASSFILE="/etc/passwd" USER_NAME="unknown" ID="unknown" output_results_json() { echo "{" echo " \"username\": \"${USER_NAME}\"," echo " \"id\": \"${ID}\"" echo "}" } output_results() { do_json=${1} if [ ${do_json} -eq 1 ]; then output_results_json else echo "username:${USER_NAME}" echo "id:${ID}" fi } set_user_info() { if [ -f ${LOGINDEFS} ]; then ID=$(cat ${LOGINDEFS} | grep ^UID_MIN | tr -s " " | cut -d' ' -f2) fi if [[ ${ID} != "unknown" && -f ${PASSFILE} ]]; then # The 3rd token is the UID IDLINE=$(cat ${PASSFILE} | cut -d':' -f3 | grep -w -in ${ID} | cut -d':' -f1) if [ ! -z ${IDLINE} ]; then USER_NAME=$(sed "${IDLINE}q;d" ${PASSFILE} | cut -d':' -f1) fi fi } ##### MAIN ##### source ${MY_PATH}/general_funcs.bash arg_has_json "$@" if [ $? -eq 0 ]; then DO_JSON=1 fi set_user_info output_results ${DO_JSON} exit 0