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

Fredrik
  • 11
  • 3

2 Answers2

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