LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-26-2006, 12:50 PM   #1
blanny
Member
 
Registered: Dec 2002
Location: CA,USA
Distribution: RHCE in training :)
Posts: 57

Rep: Reputation: 15
piping commands


Hi I'm looking to pipe a few commands to generate a userlist from /etc/passwd. I want to get only users with valid shells and exclude system accounts. I would check UID's but there are superusers in the same range as the system accounts. Here's what I have so far. Any suggestions on how I can use awk in a grep command?

cat passwd | grep -e "/bin/bash" | awk 'BEGIN { FS=":" } { print $1 }' | grep -v "awk '{ print $1 }' /etc/services"


This doesn't quite work because mysql is still in the list. What am I doing wrong?
 
Old 07-26-2006, 02:20 PM   #2
ugenn
Member
 
Registered: Apr 2002
Posts: 549

Rep: Reputation: 30
All that piping is really unecessary. awk alone can do what (what I think) you want.

awk -F: '{ if ( $6 == "/bin/bash" ) print $1 }'

ps: this assumes your definition of "valid shell" is just limited to /bin/bash.

pps: system accounts are usually created below a certain UID value. something is probably wrong on your box if system accounts are interleaved with user accounts.
 
Old 07-26-2006, 02:29 PM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Hi,

This is not as easy as it looks. As you already noticed, there are system accounts with a login shell.
Your original idea, using the UID, could be used. It will fail also if system users (with a /bin/bash shell) are added after normal users.

Root always has uid 0 and normal users have a uid range starting a lot higher (some it is 100 and up, some 500). All other (system) users are between 1 and say 99 (100 being the first uid used for a normal user).

Something like this would work in this situation:

awk 'BEGIN { FS=":" } $3 == 0 || $3 > 99 { print $1 }' /etc/passwd

This checks to see if field 3 (uid field in passwd) is 0 (zero) or larger then 99 if so it will print field one (user).

You could even include a check for a valid shell:

awk 'BEGIN { FS=":" } ( $3 == 0 || $3 > 99 ) && ( $7 == "/bin/bash" ) { print $1 }' /etc/passwd

Field 7 (shell) must also be /bin/bash. The rest of the command is the same.

PS: If a user is added (normal or otherwise) the uid is increased by one, so if you add a system user after the normal users are added (and don't include a low uid) the above command will fail if the system user has a valid bash shell.

Hope this clears things up.

Last edited by druuna; 07-26-2006 at 02:30 PM.
 
Old 07-26-2006, 02:31 PM   #4
blanny
Member
 
Registered: Dec 2002
Location: CA,USA
Distribution: RHCE in training :)
Posts: 57

Original Poster
Rep: Reputation: 15
Thanks for your reply ugenn, nice solution. Unfortunately I need to use pipes for this class right now. I guess the box is pretty messy. Some of the system accounts have shells but I don't want to include them. Do you know how I would incorporate awk into the grep expression?

I have a few others I want to try like making a MTA alias for the userlist I made:

cat passwd | grep -e "/bin/bash" | awk 'BEGIN { FS=":" } { print $5 " <"$1"@cs.sierracollege.edu>" }'

but some of the users have their phone numbers in the name column like

Jason Linder,CS 50,(916)777-7777,(916)555-5555 <un269@cs.sierracollege.edu>

How could I get only the name first and last using one long piped expression?
 
Old 07-26-2006, 02:45 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Hi again,

The pipe might not be long enough, but here we go:

cat passwd | grep "/bin/bash" | awk -F: '$5 ~ /cs.sierracollege.edu/ { print $1 }'

The above checks to see if the string cs.sierracollege.edu is present anywhere in field 5. If so it prints field one.

Hope this helps.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with piping mijohnst Linux - General 7 10-21-2005 04:14 PM
Need help with commands and piping basselstine Linux - General 1 10-02-2005 09:43 PM
vim: Piping through multiple external commands tragos Linux - Software 2 03-19-2005 01:09 PM
Piping video out to the TV only. dredgemortle Mandriva 3 07-05-2004 12:48 AM
piping commands john8675309 Linux - Software 3 12-06-2003 07:42 AM


All times are GMT -5. The time now is 01:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration