Operating System - HP-UX
1837524 Members
3648 Online
110117 Solutions
New Discussion

Re: USER LOGIN NAME SHELL SCRIPT

 
SOLVED
Go to solution
Andrew Luis Arruza
Frequent Advisor

USER LOGIN NAME SHELL SCRIPT

I know this is an easy one for someone.

I am trying to write a shell script to find out, for example, how many users have a login name with less than 4 characters and then those that have 4 or more characters.
Thanks for any/all help.
Andy
It is, after all, a matter of survival!!
4 REPLIES 4
Kofi ARTHIABAH
Honored Contributor
Solution

Re: USER LOGIN NAME SHELL SCRIPT

here you are - an awk script to do what you want ...

create a file from the lines below and call it as shown

#!/usr/bin/awk
#
# Awk file to count number of users in /etc/passwd that have
# less than 4 char user names and those with 4 or more
#
# invoke with awk -f countusers /etc/passwd

BEGIN { FS = ":" ; lessthan = 0 ; morethan = 0 }
{
if ( length ($1) < 4 )
lessthan++
else
morethan++
}
END { print "number of users with less than 4 characters " lessthan " \n";
print "number of users with more 4 or more chars " morethan ; }

nothing wrong with me that a few lines of code cannot fix!
James R. Ferguson
Acclaimed Contributor

Re: USER LOGIN NAME SHELL SCRIPT

Andrew:

If, for instance, you want this based on the active user count, try this:

# who|awk '{if (length($1)>=4) LSS4++; else GTR4++}; END {print "LSS=">LSS,"GTR="GTR}'

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: USER LOGIN NAME SHELL SCRIPT

Andrew:

Oops:

# who|awk '{if (length($1)>=4) LSS4++; else GTR4++}; END {print "LSS="LSS4,"GTR="GTR4}'

...JRF...

Alan Riggs
Honored Contributor

Re: USER LOGIN NAME SHELL SCRIPT

To correct for NIS wildcards:

grep -v [-+] /etc/passwd| awk -F: '{if (length($1)>=4) LSS4++; else GTR4++};END{
print "LSS="LSS4,"GTR="GTR4}'