#!/bin/bash #### Add new records to file "pres" #### file=pres ## Assign an ID number. touch $file if test -w then let number=`wc -l $file` let id=$number+1 else echo "File $file cannot be written!" exit fi 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. if test "$3" = "" then first="$1" middle="" # No middle name last="$2" else first="$1" 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 ## Inauguration year. echo -n "Year: " read year if test "$year" -ge 1788 then echo "ERROR: Must be a year greater than 1788"; exit fi ## Now, get the age at inauguration. echo -n "Year of birth: " read born if test $born -le 0 then echo "ERROR: Must be a year greater than zero."; exit fi let age=$year-$born echo "Age at inauguration: $age" ## Job (party) echo -n "Party: " read party ## Construct the record. record="$year:$name:$id:$age:$party" echo "ADD RECORD $id: $record" echo $record >> $file # Append to file echo `wc -l $file` records on file. exit