| 1 | #!/bin/bash |
| 2 | # realname.sh |
| 3 | # |
| 4 | # From username, gets "real name" from /etc/passwd. |
| 5 | |
| 6 | |
| 7 | ARGCOUNT=1 # Expect one arg. |
| 8 | E_WRONGARGS=85 |
| 9 | |
| 10 | file=/etc/passwd |
| 11 | pattern=$1 |
| 12 | |
| 13 | if [ $# -ne "$ARGCOUNT" ] |
| 14 | then |
| 15 | echo "Usage: `basename $0` USERNAME" |
| 16 | exit $E_WRONGARGS |
| 17 | fi |
| 18 | |
| 19 | file_excerpt () # Scan file for pattern, |
| 20 | { #+ then print relevant portion of line. |
| 21 | while read line # "while" does not necessarily need [ condition ] |
| 22 | do |
| 23 | echo "$line" | grep $1 | awk -F":" '{ print $5 }' |
| 24 | # Have awk use ":" delimiter. |
| 25 | done |
| 26 | } <$file # Redirect into function's stdin. |
| 27 | |
| 28 | file_excerpt $pattern |
| 29 | |
| 30 | # Yes, this entire script could be reduced to |
| 31 | # grep PATTERN /etc/passwd | awk -F":" '{ print $5 }' |
| 32 | # or |
| 33 | # awk -F: '/PATTERN/ {print $5}' |
| 34 | # or |
| 35 | # awk -F: '($1 == "username") { print $5 }' # real name from username |
| 36 | # However, it might not be as instructive. |
| 37 | |
| 38 | exit 0 |