So I'm trying to list a bunch of info about users in etc/passwd. So far I managed to it so that the script writes out all users starting from uid 1000. I need another function to write info about a specific user, for example: Bash myprogram.sh -ul Erik and then the script will list username, password, uid, guid, comment, directory and shell of that specific user. In the program I use $1 for the command and $2 for the second input, which will be the username
2 Answers
One could do this with getent
and awk
…
getent passwd Erik | awk -F: '{print "Username: " $1 "\nPassword: " $2 "\nUid: " $3 "\nGid: " $4 "\nComment: " $5 "\nHome: " $6 "\nShell: " $7 "\n"}'
… but it's time to remember an old command, and that not everything is an exercise in awk
or perl
:
finger -l -k Erik
Some operating systems might not have a -k
option, note. (It excludes some information that you have not expressed an interest in.) But -l
is fairly universal.
Further reading
-
I like the getent aproach, as it will catch other user databases (like NIS and LDAP/Active Directory). Feb 14, 2020 at 16:03
Try something around this:
awk -F: '{if ($3 >= 1000) {print "Username: " $1 "\nPassword: " $2 "\nUid: " $3 "\nGid: " $4 "\nComment: " $5 "\nHome: " $6 "\nShell: " $7 "\n---"}} /etc/passwd
-
1FWIW the
{if ($3 >= 1000)
can be stripped down to just$3>=1000 {
and the final}
removed.– steveFeb 13, 2020 at 19:47