LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 09-15-2007, 07:27 AM   #1
milestone
LQ Newbie
 
Registered: Sep 2007
Posts: 1

Rep: Reputation: 0
Question Shell script to list all users in /etc/passwd file


Hi,

I need a shell script which lists all users listed in /etc/passwd file. I don't know how to extract them. Please help me to write this type of script.

Is it possible to get the users with different shell from the same script? Please reply...
 
Old 09-15-2007, 07:45 AM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 326Reputation: 326Reputation: 326Reputation: 326
It's not a script, just one command:

Code:
awk -F: '{print $1}' /etc/passwd
If you want users with a specific shell:

Code:
grep "/sbin/nologin" /etc/passwd | awk -F: '{print $1}'
 
Old 09-15-2007, 10:08 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Originally Posted by macemoneta View Post
Code:
grep "/sbin/nologin" /etc/passwd | awk -F: '{print $1}'
No need for grep. awk does it all.
Code:
awk -F":" '$7 ~ /\/bin\/bash/ {print $1}' /etc/passwd
 
Old 09-15-2007, 12:24 PM   #4
daemonkl
LQ Newbie
 
Registered: Aug 2007
Posts: 28

Rep: Reputation: 15
Question

then if I wanted to make this as a command, how should i proceed?
i've tried to put the command in a file name "list" and chmod +x list
Code:
#!/bin/sh
awk -F: '{print $1}' /etc/passwd
when I issue command list:

[user@slackware:~]$ list
-bash: list: command not found

i'm still new to shell scripting

Last edited by daemonkl; 09-15-2007 at 12:26 PM.
 
Old 09-15-2007, 12:39 PM   #5
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 326Reputation: 326Reputation: 326Reputation: 326
You need to put the script in a directory in your PATH, or specify an explicit path. To see your current PATH enter:

echo $PATH

To run the 'list' command from the current directory:

./list
 
Old 09-15-2007, 12:44 PM   #6
daemonkl
LQ Newbie
 
Registered: Aug 2007
Posts: 28

Rep: Reputation: 15
thanks macemoneta,

i have moved the "list" file to /home/user/bin

instead of issuing ./list, i can now fire "list" without the ./ anywhere

thanks again!
 
Old 09-15-2007, 12:57 PM   #7
HowDoIProgramIt
Member
 
Registered: Nov 2006
Location: East Coast, USA (in "the great northeast")
Distribution: Custom / from source; Fedora, Debian, CentOS, Scientific; LFS.
Posts: 94

Rep: Reputation: 15
Quote:
Originally Posted by daemonkl View Post
then if I wanted to make this as a command, how should i proceed?
I would make it a shell function:
Code:
function ListAllUsers()
{
   awk -F: '{print $1}' /etc/passwd
}
The "function" keyword before ListAllUsers is optional; I personally don't usually include it.

If you wanted to have it available each time you logged in, you could put it in your ~/.bashrc; if you have several functions you want to put in your .bashrc, you're probably better off putting them in a separate file, such as ~/myfunctions, and putting either
Code:
. ~/myfunctions
or
Code:
source ~/myfunctions
in your .bashrc (the two commands are equivalent).

FWIW, for files that I only want to source, I don't put a "she-bang" at the start of them, or mark them +x; and, I don't use the same extension for them that I do for out-right scripts. That tends to save me at least some grief.

- Larry
 
  


Reply

Tags
scripting, shell


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SAMBA getent passwd command doesnt list all the users loganking Linux - Software 0 06-14-2006 11:22 AM
how to list all users in shell? binary_0011 Linux - Newbie 2 05-02-2006 11:40 AM
Delete group users from /etc/passwd SCRIPT wesleywest Linux - General 5 03-08-2005 09:59 AM
Looking for addusesr&passwd shell script szklanka Programming 1 02-22-2005 08:37 PM
Shell Script: Add Users from file flobadon Programming 3 12-07-2004 03:49 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration