O'Reilly logo

Linux and Unix Shell Programming by David Tansley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required