22.1. Adding records

The tasks involved in adding a record to a file are the following:

1.
Validate the input.
2.
Write the record(s) to a file.

The first task we need to do is put some functions together that can tell us whether the fields are numeric or character based and the length of the fields. This will be our data input validation, which will be used with records that are added as well as amended. Luckily we already have some of these functions at hand.

Function to check the length of a string:

length_check() 
{
# length_string 
# $1=string, $2= length of string not to exceed this number 
_STR=$1 
_MAX=$2 
_LENGTH=`echo $_STR |awk '{print length($0)}'` 
if [ "$_LENGTH" -gt "$_MAX" ]; then 
  return 1 
else 
  return 0 
fi 
}

Function to check ...

Get Linux and Unix Shell Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.