Article 5EBQA Local and "while read rec[n]"

Local and "while read rec[n]"

by
Arct1c_f0x
from LinuxQuestions.org on (#5EBQA)
Consider the following code

I can understand everything in it except for two commans/statements

the use of "local" command and the statement "while read rec[n]"

I know read is used to get user input but what about local? Is it being used to declare a local variable?

Code:

#!/bin/bash
E_DIR_NOMATCH=50
E_BAD_DBFILE=51
dbfile=Filerec.md5
# storing records.
set_up_database ()
{
echo ""$directory"" > "$dbfile"
# Write directory name to first line of file.
md5sum "$directory"/* >> "$dbfile"
# Append md5 checksums and filenames.
}
check_database ()
{
local n=0
local filename
local checksum
if [ ! -r "$dbfile" ]
then
echo "Unable to read checksum database file!"
exit $E_BAD_DBFILE
fi

while read rec[n]
do
directory_checked="${rec[0]}"
if [ "$directory_checked" != "$directory" ]
then
echo "Directories do not match up!"
# Tried to use file for a different directory.
exit $E_DIR_NOMATCH
fi
if [ "$n" -gt 0 ]
then
filename[n]=$( echo ${rec[$n]} | awk '{ print $2 }' )
# md5sum writes recs backwards,
#+ checksum first, then filename.
checksum[n]=$( md5sum "${filename[n]}" )
if [ "${rec[n]}" = "${checksum[n]}" ]
then
echo "${filename[n]} unchanged."
else
echo "${filename[n]} : CHECKSUM ERROR!"
fi
fi
let "n+=1"
done <"$dbfile" # Read from checksum database file.
}
if [ -z "$1" ]
then
directory="$PWD" # If not specified,
else
directory="$1"
fi
clear
if [ ! -r "$dbfile" ]
then
echo "Setting up database file, \""$directory"/"$dbfile"\".";
echo
set_up_database
fi
check_database
echo
exit 0Thankslatest?d=yIl2AUoC8zA latest?i=qzoFgdmeghc:33pfZaYKgq0:F7zBnMy latest?i=qzoFgdmeghc:33pfZaYKgq0:V_sGLiP latest?d=qj6IDK7rITs latest?i=qzoFgdmeghc:33pfZaYKgq0:gIN9vFwqzoFgdmeghc
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments