#!/bin/bash # vi:set ts=8 sw=4 et sta: # # Author: Clark J. Wang # # $Date$ # $HeadURL$ # $Revision$ # #--------------------------------------------------------------------# function BEGIN { export record_file=records add_file=add delete_file=delete exit_if_file_not_found "$record_file" "$add_file" "$delete_file" } #--------------------------------------------------------------------# function exit_if_file_not_found { local file= while [ $# -gt 0 ]; do file=$1 shift if [ ! -e "$file" ]; then echo "Required file '$file' does not exist." exit 1 fi done } #--------------------------------------------------------------------# function wait_enter_with_msg { local msg=$1 echo -en "$msg" read } #--------------------------------------------------------------------# function print_all_records { sed -e 's/:/ /g' "$record_file" } #--------------------------------------------------------------------# function print_all_records_formatted { awk ' BEGIN { FS = ":"; } { printf "%-10s %-10s %-5s %s %-3s %-20s %s\n", $2, $3, $4, $1, $5, $6, $7; } ' $record_file | sort } #--------------------------------------------------------------------# function print_names_and_phone { awk 'BEGIN { FS = ":"; } { printf "%s,%s,%s\n", $2, $3, $1; }' $record_file } #--------------------------------------------------------------------# function print_names_and_phone_formatted { awk 'BEGIN { FS = ":"; } { printf "%-10s %-10s %s\n", $2, $3, $1; }' $record_file } #--------------------------------------------------------------------# function search_by_name { local keyword= while true; do echo -n "Enter keyword: " read keyword if [ ! "$keyword" ]; then echo "keyword not entered" continue fi if ! grep -i "$keyword" $record_file 2> /dev/null; then echo "$keyword not found" fi return done } #--------------------------------------------------------------------# function display_main_menu { clear cat << END Employees Info Main Menu ======================== 1 - Print All Current Records 2 - Print All Current Records (formatted) 3 - Print Names and Phone Numbers 4 - Print Names and Phone Numbers (formatted) 5 - Search for specific Record(s) 6 - Add New Records 7 - Delete Records Q - Quit END echo -n "Your Selection: " } #--------------------------------------------------------------------# function END { exit 0 } #-- main ------------------------------------------------------------# { BEGIN while true; do display_main_menu read user_selection case "$user_selection" in 1) { print_all_records wait_enter_with_msg "\nPress Enter to continue..." };; 2) { print_all_records_formatted wait_enter_with_msg "\nPress Enter to continue..." };; 3) { print_names_and_phone wait_enter_with_msg "\nPress Enter to continue..." };; 4) { print_names_and_phone_formatted wait_enter_with_msg "\nPress Enter to continue..." };; 5) { search_by_name wait_enter_with_msg "\nPress Enter to continue..." };; 6) { ./$add_file };; 7) { ./$delete_file };; q|Q) { END };; "") { wait_enter_with_msg "Selection not entered. Press Enter to continue..." };; *) { wait_enter_with_msg "Invalid code! Press Enter to continue..." };; esac done END }