#!/bin/bash #### Add new records to file "emp" #### file="emp" ## Assign an ID number. touch $file let number=`wc -l $file` let id=$number+1 if [ "$1" = "" ] || [ "$2" = "" ] then echo USAGE: echo " addname First Middle Last" echo " addname First Last" fi ## Check args if test $# -gt 3 then echo "ERROR: too many arguments (2 or 3 only)"; exit fi # if test -z $1 || test -z $2 then echo "ERROR: addname requires two or three arguments."; exit fi ## Extract the name. first=$1 if test "$3" = "" then middle="" # No middle name last=$2 else middle="$2" last=$3 fi name="$first:$middle:$last" ## Check for duplicate name. echo NAME: $name grep "$name" $file dup=`grep "$name" $file` if test -n "$dup" then echo "ERROR: Duplicate name: $name" exit fi ## Now, get the phone number. echo -n "Phone number: " read phone if test $phone = 0 then echo "ERROR: Phone number cannot be zero!"; exit fi ## Year hired echo -n "Year hired: " read hired if test $hired = 0 then echo "ERROR: Must be a year greater than zero."; exit fi ## Get birth year; compute age when hired.) echo -n "Birth year: " read born if test $born -le 0 then echo "ERROR: Must be a year greater than zero."; exit fi let age=$hired-$born echo "Age when hired: $age" ## Job echo "Job" read job ## Construct the record. record="$phone:$name:$id:$hired:$age:$job" echo $record echo $record >> $file # Append to file echo `wc -l $file` records on file. exit