LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Need some help on bash script to compare /etc/passwd to locked users (https://www.linuxquestions.org/questions/linux-software-2/need-some-help-on-bash-script-to-compare-etc-passwd-to-locked-users-768413/)

rhbegin 11-11-2009 07:56 AM

Need some help on bash script to compare /etc/passwd to locked users
 
I need some assistance with a bash script:

I want to read the /etc/passwd file with only the users and
print out if it gets a match on the locked user file.

What would be the best way to go about this?

Read in all users, compare to locked users, and if it matches
then print out all matched user names.

Any help would be great.

thanks,

pixellany 11-11-2009 08:20 AM

Here's a start:

Suppose that the "locked" file is also in /etc

cd to /etc

Code:

for user in $(sed 's/\:.*//' passwd);do grep -o $user locked;done

druuna 11-11-2009 08:24 AM

Hi,

I'm not sure what it is you need to accomplish in the end, but the following command will show all locked users:

passwd -Sa | awk '( $2 == "LK" )'
The "LK" part could be "L", that depends on your system.

Hope this helps.

rhbegin 11-11-2009 08:27 AM

I actually took the users from the Server and just have the
user names in one file.

The other file is from a database with only user names in which
I was wondering would this work for these 2 files and only
print out the ones that matched?

thanks

rhbegin 11-11-2009 08:35 AM

Quote:

Originally Posted by druuna (Post 3752945)
Hi,

I'm not sure what it is you need to accomplish in the end, but the following command will show all locked users:

passwd -Sa | awk '{ $2 == "LK" }'
The "LK" part could be "L", that depends on your system.

Hope this helps.

Would I run this against the /etc/passwd or could I use
a file?

druuna 11-11-2009 08:55 AM

Hi,

The command shown will give you a list of users that are locked (to my knowledge /etc/passwd and /etc/shadow are used).

If you need the users only, you need to extend the command:

passwd -Sa | awk '( $2 == "LK" ) { print $1 }'

PS: I made a typo in post #3: { should be ( and } should be )
Edited post #3....

rhbegin 11-11-2009 09:32 AM

passwd -Sa | awk '( $2 == "LK" ) { print $1 }'
passwd: bad argument -Sa: unknown option

druuna 11-11-2009 10:48 AM

Code:

passwd -Sa | awk '( $2 == "L" ) { print $1 }'
fcron
lp
smmsp
messagebus
stunnel
apache
slapd
mysql
proftpd
sshd


rhbegin 11-11-2009 12:55 PM

Thank you for your help, I was not familiar with passwd -S user
to display it being locked.

Thanks again!

:)


All times are GMT -5. The time now is 04:33 AM.