3

我需要询问用户名并打印与该用户名关联的名称和主目录路径

echo Please enter your username
read username

我不确定如何使用 $username 来查找有关其主目录和名称的信息。

4

3 回答 3

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

于 2013-01-15T21:13:51.043 回答
1
echo Please enter a username
read username

cat /etc/passwd | grep "$username" | cut -d ":" -f6
cat /etc/passwd | grep "$username" | cut -d ":" -f5
于 2013-01-15T21:57:31.283 回答
1

采用getent

read -p "username: " username
IFS=: read username password uid gid gecos homedir shell < <(getent passwd "$username")
echo "fullname=${gecos%%,*}"
echo "homedir=$homedir"
于 2013-01-15T22:15:05.690 回答