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
Asked
Active
Viewed 479 times
2 Answers
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
JdeBP
- 66,967
- 12
- 159
- 343
-
I like the getent aproach, as it will catch other user databases (like NIS and LDAP/Active Directory). – JucaPirama Feb 14 '20 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
steve
- 21,582
- 5
- 48
- 75
JucaPirama
- 333
- 1
- 10
-
1FWIW the `{if ($3 >= 1000)` can be stripped down to just `$3>=1000 {` and the final `}` removed. – steve Feb 13 '20 at 19:47