#!/bin/bash # vi:set ts=8 sw=4 et sta: # # Author: Clark J. Wang # # $Date$ # $HeadURL$ # $Revision$ # #--------------------------------------------------------------------# function trim_spaces { if [ $# = 0 ]; then return fi local var_name=$1 local var_value=$(eval "echo \"\$$var_name\"") var_value=$(echo "$var_value" | sed -e 's/^[ \t]\+//') var_value=$(echo "$var_value" | sed -e 's/[ \t]\+$//') eval "$var_name=\"$var_value\"" } #--------------------------------------------------------------------# function BEGIN { if [ ! -e "$record_file" ]; then echo "$record_file not found" exit 1 fi } #-- main ------------------------------------------------------------# { BEGIN while true; do clear cat << END Employee Info Additions ======================= Enter the following details of the new employee: Phone Number Last Name First Name Middle Init Dept # Job Title Date Hired END # get the phone number while true; do echo -n "Phone Number (xx-xxxxxxxx): " read phone_number trim_spaces phone_number if [ ! "$phone_number" ]; then echo "Phone number not entered" continue fi if ! echo $phone_number | grep -Eq '^[0-9]{2}-[0-9]{8}$'; then echo "Invalid phone number" continue fi break done # get the last name while true; do echo -n "Last Name: " read last_name trim_spaces last_name if [ ! "$last_name" ]; then echo "Last name not entered" continue fi if ! echo $last_name | grep -Eq '^[a-zA-Z ]+$'; then echo "Last name can contain only alphabets and spaces" continue fi break done # get the first name while true; do echo -n "First Name: " read first_name trim_spaces first_name if [ ! "$first_name" ]; then echo "First name not entered" continue fi if ! echo $first_name | grep -Eq '^[a-zA-Z ]+$'; then echo "First name can contain only alphabets and spaces" continue fi break done # get the middle init while true; do echo -n "Middle Init: " read middle_init trim_spaces middle_init if [ ! "$middle_init" ]; then echo "Middle Init not entered" continue fi if ! echo $middle_init | grep -Eq '^[a-zA-Z ]+$'; then echo "Middle Init can contain only alphabets and spaces" continue fi break done # get the Dept # while true; do echo -n "Dept #: " read dept_no trim_spaces dept_no if [ ! "$dept_no" ]; then echo "Dept # not entered" continue fi if ! echo $dept_no | grep -Eq '^[0-9]+$'; then echo "Dept # can contain only digits" continue fi break done # get the Job Title while true; do echo -n "Job Title: " read job_title trim_spaces job_title if [ ! "$job_title" ]; then echo "Job Title not entered" continue fi if ! echo $job_title | grep -Eq '^[a-zA-Z ]+$'; then echo "Job Title can contain only alphabets and spaces" continue fi break done # get the Date Hired while true; do echo -n "Date Hired (dd-mm-yyyy): " read date_hired trim_spaces date_hired if [ ! "$date_hired" ]; then echo "Date Hired not entered" continue fi if ! echo $date_hired | grep -Eq '^[0-9]{2}-[0-9]{2}-[0-9]{4}$'; then echo "Invalid Date Hired" continue fi dd=${date_hired:0:2} mm=${date_hired:3:2} yy=${date_hired:6:4} if [ $dd -lt 1 -o $dd -gt 31 -o $mm -lt 1 -o $mm -gt 12 -o $yy -lt 1000 -o $yy -gt 9999 ]; then echo "Invalid Date Hired" continue fi break done if echo "$phone_number:$last_name:$first_name:$middle_init:$dept_no:$job_title:$date_hired" >> $record_file; then echo "Record Saved" fi echo -n "Add another? (y)es or (n)o: " read confirm if [ "$confirm" = y -o "$confirm" = Y ]; then continue else break fi done }