Linux makes it very easy to get information about the user accounts on your system. Using awk it is simple to filter the information.
┗━━━━━━━━━━┓ john@localhost ~ ┗━━━━━━━━━━━━━╾ ╍▷ awk -F: '{if ( $3 > 1000 && $3 < 3000) print $1,"\t "$5"\t ",$7,"\t "$4}' /etc/passwd joan Joan Collins /bin/bash 1001 jim James Kirk /bin/bash 1002 |
This show that it is possible to use just awk to filter information and it is not necessary to use other tools in pipes. Awk can do everything.
Here is another example. This will print a listing of all actual users that can log in on the Linux system.
┗━━━━━━━━━━┓ john@localhost ~ ┗━━━━━━━━━━━━━╾ ╍▷ awk -F: '$6 ~ "^/home/" && $4 > 999 {print $1,"\t"$3,"\t\t"$5,"\t\t"$6}' /etc/passwd john 1000 John Cartwright /home/john joan 1001 Joan Collins /home/joan jim 1002 James Kirk /home/jim |
And this is yet another example.
┗━━━━━━━━━━┓ john@localhost ~ ┗━━━━━━━━━━━━━╾ ╍▷ getent passwd | awk -F: '$3 > 999 && $3 < 3000 { print $1,"\t"$2,"\t"$3,"\t"$4,"\t"$5,"\t"$6 }' john x 1000 1000 John Cartwright /home/john joan x 1001 1001 Joan Collins /home/joan jim x 1002 1002 James Kirk /home/jim |
It is also possible to use a C program to get information about all users with an ID greater than 999 and less than 3000.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <pwd.h> int main() { struct passwd *p; while ((p = getpwent()) != NULL) { if (p->pw_uid > 999 && p->pw_uid < 3000) { printf("\tUsername: %s\n", p->pw_name); printf("\tUID: %d\n", p->pw_uid); printf("\tHome Directory: %s\n", p->pw_dir); printf("\n"); } } endpwent(); return 0; } |
This program works perfectly. This is an easy way to list simple user information.
Compile this program like this.
┗━━━━━━━━━━┓ john@localhost ~/Documents ┗━━━━━━━━━━━━━╾ ╍▷ gcc -Wall -Os users.c -o users |
This is what the output looks like.
┗━━━━━━━━━━┓ john@localhost ~/Documents ┗━━━━━━━━━━━━━╾ ╍▷ ./users Username: john UID: 1000 Home Directory: /home/john Username: joan UID: 1001 Home Directory: /home/joan Username: jim UID: 1002 Home Directory: /home/jim |