Find the answer to your Linux question:
Results 1 to 10 of 10
Can nebody tell me a command ( or combo of cmds, so that I can put it in a shell script ) to view list of ALL users ( may ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Jul 2004
    Location
    Calcutta, India
    Posts
    220

    command to view list of ALL users and groups


    Can nebody tell me a command ( or combo of cmds, so that I can put it in a shell script ) to view list of ALL users ( may or may NOT be logged in, preferably non-system users only ) ??
    and something similar for a list of ALL groups ??

    I need it badly but cant really figure it out ...

  2. #2
    Linux Engineer
    Join Date
    Apr 2005
    Location
    Buenos Aires, Argentina
    Posts
    908
    cat /etc/passwd | cut -d: -f1
    cat /etc/group |cut -d: -f1
    serzsite.com.ar
    "All the drugs in this world won\'t save you from yourself"

  3. #3
    Linux Newbie
    Join Date
    Jul 2004
    Location
    Calcutta, India
    Posts
    220
    ok .. that is fine ...
    now a little bit more .. the output of cat /etc/passwd | cut -d: -f1
    shows system users as well .. I want to show only the non-system users,
    like my own user account, but NOT root, etc.
    ie., on my system, general users have UIDs starting frm 500, so I just wanna check the 3rd field of each line in the passwd file and accordingly print them.

    Please help someone, I know this can be done (probably using awk or sed), bcos I had seen similar things before, though I dont remember now.

    Thanks for everything so far..

  4. #4
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Code:
    #!/usr/bin/perl -w
    
    open PASSWD, "/etc/passwd" or die "$0: Cannot Open File /etc/passwd For Input!!\n";
    
    while&#40;<PASSWD>&#41;
    &#123;
        /^&#40;\w+&#41;&#58;x&#58;&#40;\d+&#41;/;
    
        if&#40;$2 > 500&#41;
        &#123;
            print "$1\n";
        &#125;
    &#125;
    Heh, I tried writing it in Bash first (and it almost worked!), but here it is in Perl, which I actually know .

  5. #5
    Linux Newbie
    Join Date
    Jul 2004
    Location
    Calcutta, India
    Posts
    220
    Just figured out another one ...
    Code:
    #!/bin/sh
    
    awk -F&#58; '&#123;if &#40;$3>=500 && $3<=1000&#41; print&#125;' /etc/passwd | cut -d&#58; -f1
    The $3<=1000 is for the nfsnobody user a/c which has an UID of 65534.

    Thanks anyway Cabhan, I have already copied your perl soln and added to my archives.

  6. #6
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    I think that just everyone hates me .

    Hehe, everytime I write some sort of script, someone replies with like a two-line Bash command. Damn you all.

  7. #7
    Linux Newbie
    Join Date
    Jul 2004
    Location
    Calcutta, India
    Posts
    220
    hmmm .. well actually its NOT that I do not like perl. I simply have never really had the enthusiasm and time to learn it well, as of now. however, I do know about the various capabilities of perl and unless I would have got that soln before checking your reply, I would have stuck to your version only.
    But then, as I have said already, I do collect scripts which look interesting to me, and yours is definitely one of them.

  8. #8
    Linux Guru bigtomrodney's Avatar
    Join Date
    Nov 2004
    Location
    Ireland
    Posts
    6,124
    Man I am really gonna have to get familiar with gawk. Every time I thinkg I get an idea someone else shows me it at work and I go 'cool'. Cheers for that code, will be used! Although a lot of my new users have much higher UIDs so I've modified it a bit.

  9. #9
    Linux Newbie
    Join Date
    Jul 2004
    Location
    Calcutta, India
    Posts
    220
    an even shorter one, dunno y it did not come to my mind earlier :
    Code:
    #!/bin/sh
    
    awk -F&#58; '&#123;if &#40;$3>=500 && $3<=1000&#41; print $1&#125;' /etc/passwd

  10. #10
    Banned CodeRoot's Avatar
    Join Date
    Sep 2005
    Posts
    567
    Quote Originally Posted by Cabhan
    I think that just everyone hates me.
    You are letting your imagination run wild...

    Quote Originally Posted by Cabhan
    Hehe, everytime I write some sort of script, someone replies with like a two-line Bash command.
    How about this:

    Code:
    #!/usr/bin/perl -w
    open PASSWD, "/etc/passwd" or die "$0: Cannot Open File /etc/passwd For Input!!\n";
    while(<PASSWD>) { /^(\w+):x:(\d+)/; if($2 > 500) { print "$1\n"; } }
    (It doesn't make yours less valid.)

    Quote Originally Posted by Cabhan
    Damn you all.
    "Now, that's not nice..."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •