You are not logged in.
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
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 ).
Anyways - for now:
$ man cut
Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy
Offline
This can be done using the delimiter function of cut.
grep $username /etc/passwd | cut -d ':' -f 6
Offline
grep andreas /etc/passwd | cut -d: -f6
grep andreas /etc/passwd | awk -F: '{print $6}'
Offline
Thank you, the script is now comleted
Offline
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
Silly little penguins
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
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
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
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