2013-01-15 58 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"