#!/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
}
I need to use the variable I get from the shell script and put it in the awk script to search the passwd file for the certain user and display the said information, but am unsure how it works. I don't full understand how to use the -v command and where to put it in the awk script.