mirror of https://github.com/tLDP/LDP
39 lines
884 B
Bash
39 lines
884 B
Bash
#!/bin/bash
|
|
# realname.sh
|
|
#
|
|
# From username, gets "real name" from /etc/passwd.
|
|
|
|
|
|
ARGCOUNT=1 # Expect one arg.
|
|
E_WRONGARGS=85
|
|
|
|
file=/etc/passwd
|
|
pattern=$1
|
|
|
|
if [ $# -ne "$ARGCOUNT" ]
|
|
then
|
|
echo "Usage: `basename $0` USERNAME"
|
|
exit $E_WRONGARGS
|
|
fi
|
|
|
|
file_excerpt () # Scan file for pattern,
|
|
{ #+ then print relevant portion of line.
|
|
while read line # "while" does not necessarily need [ condition ]
|
|
do
|
|
echo "$line" | grep $1 | awk -F":" '{ print $5 }'
|
|
# Have awk use ":" delimiter.
|
|
done
|
|
} <$file # Redirect into function's stdin.
|
|
|
|
file_excerpt $pattern
|
|
|
|
# Yes, this entire script could be reduced to
|
|
# grep PATTERN /etc/passwd | awk -F":" '{ print $5 }'
|
|
# or
|
|
# awk -F: '/PATTERN/ {print $5}'
|
|
# or
|
|
# awk -F: '($1 == "username") { print $5 }' # real name from username
|
|
# However, it might not be as instructive.
|
|
|
|
exit 0
|