You are not logged in.

#1 2009-05-05 20:09:38

Flasher
Member
From: Bavaria / Germany
Registered: 2005-04-24
Posts: 126

Extract homepath from /etc/passwd

Hello!

At the moment I'm writing a small shell script. I want to search after a specific username in /etc/passwd and if /etc/passwd contains this name, I want to store the path in a variable.

For example:

/etc/passwd:

root:x:0:0:root:/root:/bin/zsh
bin:x:1:1:bin:/bin:/bin/false
daemon:x:2:2:daemon:/sbin:/bin/false
mail:x:8:12:mail:/var/spool/mail:/bin/false
ftp:x:14:11:ftp:/srv/ftp:/bin/false
http:x:33:33:http:/srv/http:/bin/false
nobody:x:99:99:nobody:/:/bin/false
andreas:x:1000:1000::/home/andreas:/bin/bash
dbus:x:81:81:System message bus:/:/bin/false
hal:x:82:82:HAL daemon:/:/bin/false
policykit:x:102:1001:PolicyKit:/:/sbin/nologin
avahi:x:84:84:Avahi daemon:/:/bin/false
gdm:x:120:1002:PolicyKit:/var/lib/gdm:/sbin/nologin

I'm searching for the homepath of user "andreas":

The end-result should be:

echo $homepath
/home/andreas

I can grep the whole line with:

grep $username /etc/passwd

But I have to cut the unnecessary content before and after /home/andreas

Thanks for help!

Best regards,

Flasher

Offline

#2 2009-05-05 20:13:48

.:B:.
Forum Fellow
Registered: 2006-11-26
Posts: 5,819
Website

Re: Extract homepath from /etc/passwd

I'd say use the 'cut' command and specify : as a delimiter, but you can probably do it with awk too (I don't know how; but I bet you the next post is from someone telling how to do it with awk tongue).

Anyways - for now:

$ man cut

Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy

Offline

#3 2009-05-05 20:25:22

bobdob
Member
Registered: 2008-06-13
Posts: 138

Re: Extract homepath from /etc/passwd

This can be done using the delimiter function of cut.

grep $username /etc/passwd | cut -d ':' -f 6

Offline

#4 2009-05-05 20:28:02

crouse
Arch Linux f@h Team Member
From: Iowa - USA
Registered: 2006-08-19
Posts: 907
Website

Re: Extract homepath from /etc/passwd

grep andreas /etc/passwd | cut -d: -f6
grep andreas /etc/passwd | awk -F: '{print $6}'

Offline

#5 2009-05-05 20:48:53

Flasher
Member
From: Bavaria / Germany
Registered: 2005-04-24
Posts: 126

Re: Extract homepath from /etc/passwd

Thank you, the script is now comleted smile

Offline

#6 2009-05-05 21:09:11

djgera
Developer
From: Buenos Aires - Argentina
Registered: 2008-12-24
Posts: 723
Website

Re: Extract homepath from /etc/passwd

awk 'BEGIN {FS=":"} /^andreas:/ { print $6 }' /etc/passwd

(note the : after username)

or more elaborated

awk 'BEGIN{FS=":"} $1 ~ "^andreas$" { print $6 }' /etc/passwd

^ and $ are importan without this you will match ""*andreas*""

also can replace "BEGIN {FS=":"}" with awk -F: 'blah blah'

you decide.

Offline

#7 2009-05-05 21:14:55

Wra!th
Member
Registered: 2009-03-31
Posts: 342

Re: Extract homepath from /etc/passwd

Silly little penguins smile

awk -F: '/andreas/ {print $6}' /etc/passwd

MacGregor DESPITE THEM!
7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00

Offline

#8 2009-05-05 21:24:06

markp1989
Member
Registered: 2008-10-05
Posts: 431

Re: Extract homepath from /etc/passwd

why not just "echo $HOME"  ?


Desktop: E8400@4ghz - DFI Lanparty JR P45-T2RS - 4gb ddr2 800 - 30gb OCZ Vertex - Geforce 8800 GTS - 2*19" LCD
Server/Media Zotac GeForce 9300-ITX I-E - E5200 - 4gb Ram - 2* ecogreen F2 1.5tb - 1* wd green 500gb - PicoPSU 150xt - rtorrent - xbmc - ipazzport remote - 42" LCD

Offline

#9 2009-05-05 21:27:55

Wra!th
Member
Registered: 2009-03-31
Posts: 342

Re: Extract homepath from /etc/passwd

markp1989 wrote:

why not just "echo $HOME"  ?

Because he wants A user's home path, not the current logged in user's.


MacGregor DESPITE THEM!
7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00

Offline

#10 2009-05-05 22:03:34

markp1989
Member
Registered: 2008-10-05
Posts: 431

Re: Extract homepath from /etc/passwd

Wra!th wrote:
markp1989 wrote:

why not just "echo $HOME"  ?

Because he wants A user's home path, not the current logged in user's.

ah i see, i miss read the question


Desktop: E8400@4ghz - DFI Lanparty JR P45-T2RS - 4gb ddr2 800 - 30gb OCZ Vertex - Geforce 8800 GTS - 2*19" LCD
Server/Media Zotac GeForce 9300-ITX I-E - E5200 - 4gb Ram - 2* ecogreen F2 1.5tb - 1* wd green 500gb - PicoPSU 150xt - rtorrent - xbmc - ipazzport remote - 42" LCD

Offline

Board footer

Powered by FluxBB