#!/usr/bin/env bash passwd_file=`mktemp` # getpwent returns duplicate entries for root and nobody users # due to a bug in systemd-userdb.service that occurs # in systemd 245 # https://github.com/systemd/systemd/issues/15160 if which systemctl &>/dev/null && \ [[ `systemctl --version | grep "systemd 245"` =~ "245" ]] ; then grep -Ev '^(root|nobody)' /etc/passwd > "$passwd_file" else case `uname` in # The first two lines of /etc/passwd on FreeBSD are not account entries FreeBSD) sed 1,2d /etc/passwd > $passwd_file ;; *) cp /etc/passwd "$passwd_file" ;; esac fi LINES_COUNT=`cat "$passwd_file" | wc -l` function getField { LINE=`sed -n "${I}p" "$passwd_file"` case $1 in 'username' ) echo $LINE | awk -F':' '{print $1}' ;; 'password' ) echo $LINE | awk -F':' '{print $2}' ;; 'user_id' ) echo $LINE | awk -F':' '{print $3}' ;; 'group_id' ) echo $LINE | awk -F':' '{print $4}' ;; 'gcos' ) echo $LINE | awk -F':' '{gsub(/&/,"&",$5); print $5}' ;; 'home_dir' ) echo $LINE | awk -F':' '{print $6}' ;; 'login_shell' ) echo $LINE | awk -F':' '{print $7}' ;; esac } cat < password 1.0 5.4 2008-03-31T00:00:00-00:00 EOF I=1 while [ $I -le $LINES_COUNT ]; do echo "" I=$[$I+1] done cat < EOF I=1 while [ $I -le $LINES_COUNT ]; do cat < EOF I=$[$I+1] done cat < EOF I=1 while [ $I -le $LINES_COUNT ]; do cat < `getField "username" ${I}` EOF I=$[$I+1] done cat < EOF I=1 while [ $I -le $LINES_COUNT ]; do cat < `getField 'username' $I` `getField 'password' $I` `getField 'user_id' $I` `getField 'group_id' $I` `getField 'gcos' $I` `getField 'home_dir' $I` `getField 'login_shell' $I` EOF I=$[$I+1] done cat < EOF