0

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 2

4

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

1
  • I like the getent aproach, as it will catch other user databases (like NIS and LDAP/Active Directory).
    – JucaPirama
    Commented Feb 14, 2020 at 16:03
1

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
1
  • 1
    FWIW the {if ($3 >= 1000) can be stripped down to just $3>=1000 { and the final } removed.
    – steve
    Commented Feb 13, 2020 at 19:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .