Find the answer to your Linux question:
Results 1 to 6 of 6
First time script writer here, I have this which lists all users and their primary + secondary groups... for user in $(getent passwd | cut -d: -f1); do printf "%s: ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    aic
    aic is offline
    Just Joined!
    Join Date
    Jun 2013
    Posts
    5

    display all users secondary groups


    First time script writer here, I have this which lists all users and their primary + secondary groups...

    for user in $(getent passwd | cut -d: -f1);
    do
    printf "%s: %s\n" $user "$(id -Gn $user)"
    done

    Is there a way I can modify this to only show the secondary groups?
    Last edited by aic; 06-25-2013 at 10:56 PM.

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,304
    Hi.

    Welcome to the forum.
    Code:
    $ id -Gn
    drl dialout cdrom floppy audio video plugdev
    Code:
    $ id -Gn|cut -d" " -f2-
    dialout cdrom floppy audio video plugdev
    See man cut for details.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  3. #3
    aic
    aic is offline
    Just Joined!
    Join Date
    Jun 2013
    Posts
    5
    Quote Originally Posted by drl View Post
    Hi.

    Welcome to the forum.
    Code:
    $ id -Gn
    drl dialout cdrom floppy audio video plugdev
    Code:
    $ id -Gn|cut -d" " -f2-
    dialout cdrom floppy audio video plugdev
    See man cut for details.

    Best wishes ... cheers, drl
    Thank you. While it does work on the users that have secondary groups, primary groups are still showing next to the users that only belong to a primary group. Is there anyway to make it so that the first field is completely ignored even when it's the only one?

  4. $spacer_open
    $spacer_close
  5. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,304
    Hi.
    Code:
    $ v1=$(echo xx yy zz|awk '{$1="";$0=$0;print}')
    $ echo $v1
    yy zz
    
    $ v1=$(echo xx yy|awk '{$1="";$0=$0;print}')
    $ echo $v1
    yy
    
    $ v1=$(echo xx |awk '{$1="";$0=$0;print}')
    $ echo $v1
    This takes advantage of the shell's behavior to eliminate (some) spaces.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  6. #5
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,304
    Hi.

    Also:
    Code:
    $ perl -e 'shift;print join(" ",@ARGV),"\n"' -- $(echo xx yy zz)
    yy zz
    
    $ perl -e 'shift;print join(" ",@ARGV),"\n"' -- $(echo xx)
    cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  7. #6
    aic
    aic is offline
    Just Joined!
    Join Date
    Jun 2013
    Posts
    5
    Quote Originally Posted by drl View Post
    Hi.
    Code:
    $ v1=$(echo xx yy zz|awk '{$1="";$0=$0;print}')
    $ echo $v1
    yy zz
    
    $ v1=$(echo xx yy|awk '{$1="";$0=$0;print}')
    $ echo $v1
    yy
    
    $ v1=$(echo xx |awk '{$1="";$0=$0;print}')
    $ echo $v1
    This takes advantage of the shell's behavior to eliminate (some) spaces.

    Best wishes ... cheers, drl
    Awesome, thanks so much!

Posting Permissions

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