All,
Working in Kubuntu 14.04
I know I can find the default user line using the cmd from bash
:
cat /etc/passwd | grep 1000
What I get is:
user:x:1000:1000:User Name,,,:/home/user:/bin/bash
How do I extract to get:
myuser=$user
myhome=$homdir
All help appreciated!
Thanks!
OMR
Aia
2
See if any of these will get you going:
awk -F":" '/1000/ {print $1, $6}' /etc/passwd
or:
awk -F":" '$3=="1000" {print $1, $6}' /etc/passwd
By the way
cat /etc/passwd | grep 1000
is like eating macaronis using a fork first to load into a spoon.
grep
can read files by itself
grep 1000 /etc/passwd
Expanding on what Aia said to get those values assigned to the desired variables:
read myuser myhome <<< $(awk -F":" '$3=="1000" {print $1, $6}' /etc/passwd)
RudiC
4
pure bash
:
while IFS=":" read user _ userid _ _ homedir _; do [ $userid -eq 1000 ] && break; done < /etc/passwd
or
IFS=":" read user _ userid _ _ homedir _ <<< $(grep ".*:.*:1000:" /etc/passwd)
This is not false positive proof, though.
Rudi, your second solution must have the here-string in quotes.
Just like the following
IFS=":" read myuser x x x x myhome x <<< "`getent passwd 1000`"
Otherwise special characters can spoil it.
And bash seems to have a parsing problem...?
RudiC
6
Works for my bash
. What special chars could spoil it? The here string would supply one single (hopefully) line that would be read into the variables.
I get this:
% cat passwd
root:x:0:0:Super-User:/:/bin/sh
user:x:1000:0:Super-User:/:/bin/sh
% IFS=":" read user _ userid _ _ homedir _ <<< $(grep ".*:.*:1000:" passwd); echo $user; echo $userid
user x 1000 0 Super-User / /bin/sh
% IFS=":" read user _ userid _ _ homedir _ <<< "$(grep ".*:.*:1000:" passwd)"; echo $user; echo $userid
user
1000
% echo $BASH_VERSION
3.2.57(1)-release
%