我需要询问用户名并打印与该用户名关联的名称和主目录路径
echo Please enter your username
read username
我不确定如何使用 $username 来查找有关其主目录和名称的信息。
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
echo Please enter a username
read username
cat /etc/passwd | grep "$username" | cut -d ":" -f6
cat /etc/passwd | grep "$username" | cut -d ":" -f5
采用getent
read -p "username: " username
IFS=: read username password uid gid gecos homedir shell < <(getent passwd "$username")
echo "fullname=${gecos%%,*}"
echo "homedir=$homedir"