0

I want to read the users from /etc/passwd file.My /etc/passwd file is

/etc/passwd

root:eRYFFjZ2./oo:0:0:root:/:/bin/bash
nobody:*:99:99:Nobody:/:
ftp:*:14:50:FTP User:/:/bin/false
#guest
guest:/QP8Q2QieMWi4:500:500:Guest:/:/bin/bash
gst1:x:1002:1003:,,,:/home/gst1:/bin/bash
#gend
#admin
ad1:x:1003:1004:,,,:/home/adm1:/bin/bash
ad2:x:1004:1005:,,,:/home/adm2:/bin/bash
#adend

I want to get users from this file using a script.first read guest and gst1 .Then ad1,ad2. i tried to use

awk'#guest/{flag-1;next}/#gend/{flag=0}flag' /etc/passwd
awk'#admin/{flag-1;next}/#adend/{flag=0}flag' /etc/passwd

this gives me two lines

guest:/QP8Q2QieMWi4:500:500:Guest:/:/bin/bash
gst1:x:1002:1003:,,,:/home/gst1:/bin/bash
ad1:x:1003:1004:,,,:/home/adm1:/bin/bash
ad2:x:1004:1005:,,,:/home/adm2:/bin/bash

I want to extract first 'guest' do some operation with it then extract 'gst1' . Then i want to extract admin users names(ad1 and ad2)

Is it possible to extract them one by one in a while loop.I want like this

start from label #guest
get user 'guest'
===do some work with 'guest'===
continue above steps for the users up to label #gend 

I am running this linux in a embedded target and the file systems are custom built .

Adhz
  • 55
  • 7

3 Answers3

1

You can use the following GNU sed command:

sed '0,/#guest/d;/#gend/,$d;s/:.*//' /etc/passwd

0,/#guest/d removes all lines until #guest (including it).

/#gend/,$d removes everything from #gend to the end of input which is expressed using a $. (in GNU sed)

s/:.*// deletes everything from the first : to the end of the line for the remaining lines.

Output:

guest
gst1

If want to "do something" with the user names, use a while loop together with read:

sed '0,/#guest/d;/#gend/,$d;s/:.*//' /etc/passwd | while read user ; do
    echo "$user"
    do something ...
done
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
0

You need to specify : as record separator and take advantage of record variables. Let's print a simple message:

 awk -F: '/#guest/{flag=1;next} /#gend/ {flag=0} flag { print "work with \"" $1 "\""}' /etc/passwd

But you may also run an external command providing the username as parameter, changing the print statement to a system call.

Eg. let's list the user home dir content (using the sixth column content of /etc/passwd):

awk -F: '/#guest/{flag=1;next} /#gend/{flag=0} flag { system("ls " $6) }' /etc/passwd
Dfaure
  • 564
  • 7
  • 26
  • this prints both users.i am looking for a way to take one at a time(guest),do some work with that user .Then take the next user(gst1) ,do the same work with that .. continue up to the end flag #gend – Adhz Jun 08 '16 at 11:30
  • feel free to replace the print statement with some other treatment, using `$2` to get the username. Could you provide more details on the kind of work you plan to do? – Dfaure Jun 08 '16 at 11:34
0

Simply put a loop around the output:

awk'#guest/{flag-1;next}/#gend/{flag=0}flag' /etc/passwd | while read pwline
do
   # do what you want to do with guest users
done

awk'#admin/{flag-1;next}/#adend/{flag=0}flag' /etc/passwd | while read pwline
do
   # do what you want to do with admin users
done
Stefan Hegny
  • 2,107
  • 4
  • 23
  • 26