#!/bin/bash
# This tells you that the script must be run by root.
if [ "$(id -u)" != "0" ];
then
echo "This script must be run as root!" 1>&2
exit 1
fi
userexists=0
while [ $userexists -eq 0 ]
do
# This asks for the user's input for a username.
echo -n "Enter a username: "
read username
x="$username:x:"
# This if block checks if the username exists.
grep -i $x /etc/passwd > /dev/null
if [ $? -eq 0 ]
then
userexists=1
else
echo "That user does not exist!"
fi
done
# This is the heading for the information to be displayed
echo "Information for" $username
echo "--------------------------------------------------------------------"
awk -v varname="var" -f passwd.awk /etc/passwd
awk -f shadow.awk /etc/shadow
BEGIN { FS = ":" }
/"variable"/{print "Username\t\t\t" $1
print "Password\t\t\tSet in /etc/shadow"
print "User ID\t\t\t\t"$3
print "Group ID\t\t\t"$4
print "Full Name\t\t\t"$5
print "Home Directory\t\t\t"$6
print "Shell\t\t\t\t"$7
}
我需要使用从 shell 脚本中获得的变量并将其放入 awk 脚本中以搜索特定用户的 passwd 文件并显示所述信息,但不确定它是如何工作的。我不完全了解如何使用 -v 命令以及将其放在 awk 脚本中的什么位置。