2013-01-15 27 views
3

我要问用户名和打印与该用户名相关的名称和主目录路径打印使用用户名

echo Please enter your username 
read username 

我不知道如何使用$用户名相关的名称和主目录路径找到有关其主目录和名称的信息。

+0

如果夏娃把她的名字命名为爱丽丝,该怎么办? –

回答

1
echo Please enter a username 
read username 

cat /etc/passwd | grep "$username" | cut -d ":" -f6 
cat /etc/passwd | grep "$username" | cut -d ":" -f5 
3

awk -F: -v v="$username" '{if ($1==v) print $6}' /etc/passwd

没有AWK:

cat /etc/passwd | grep "$username" | cut -d ":" -f6

用户名:

cat /etc/passwd | grep "$username" | cut -d ":" -f5

+0

OP也想要名字。 –

+0

对不起,但我不知道这个缩写。 OP? –

+0

原始海报。这个人提出了这个问题。 –

1

使用getent

read -p "username: " username 
IFS=: read username password uid gid gecos homedir shell < <(getent passwd "$username") 
echo "fullname=${gecos%%,*}" 
echo "homedir=$homedir"