Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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).
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.
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.