views:

26

answers:

2

I am trying to use Bourne shell scripting for the first time ever, and I cannot seem to figure out determining how to save text from a file to internal script variables. The file format is as follows:

acc.text

Jason Bourne 213.4
Alice Deweger 1
Mark Harrington 312

The current script that I have (which might be ENTIRELY incorrect as I am simply creating it in NotePad++ without using an actual shell console) is as follows:

#!/bin/sh

process_file()
{

FILE = $1;
SALARYARRAY;
NAMEARRAY;
COUNTER = 0;

while read line
   do
 $NAMEARRAY[$COUNTER] = 
    $SALARYARRAY[$COUNTER] =
 $COUNTER + 1;
 echo $NAMEARRAY[$COUNTER]:$SALARYARRAY[$COUNTER];
done < "$FILE"

order_Map 
}

# Function is not complete as of now, will later order SALARYARRAY in descending order
order_Map()
{
i = 0;
for i in $COUNTER
 do
   if ($SALARYARRAY[
 done

}

##
# Main Script Body
#
# Takes a filename input by user, passes to process_file()
##

PROGRAMTITLE = "Account Processing Shell Script (APS)"
FILENAME = "acc.$$"


echo $PROGRAMTITLE

 echo Please specify filename for processing
 read $FILENAME

 while(! -f $FILE  ||  ! -r $FILE)
   do
  echo Error while attempting to write to file. Please specify file for processing:
  read $FILENAME
   done


echo Processing the file... 
process_file $FILENAME 
A: 

I have fixed some of your script. You need to cut out the name and salary fields from each line before storing them into the array.

#!/bin/bash

process_file()
{
file=$1;
counter=0
while read line
do
        #the name is the first two fields
        name=`echo $line | cut -d' ' -f1,2`
        NAMEARRAY[$counter]="$name"

        #the salary is the third field
        salary=`echo $line | cut -d' ' -f3`
        SALARYARRAY[$counter]="$salary"

        echo ${NAMEARRAY[$counter]}:${SALARYARRAY[$counter]}

        counter=$(($counter+1))
done < $file
}


##
# Main Script Body
#
# Takes a filename input by user, passes to process_file()
##

PROGRAMTITLE="Account Processing Shell Script (APS)"
echo $PROGRAMTITLE

echo -n "Please specify filename for processing: "
read FILENAME

if [ -r $FILENAME ] #check that the file exists and is readable
then
        process_file $FILENAME
else
        echo "Error reading file $FILENAME"
fi
dogbane
A: 

Given the file format you specified, each record has three fields first last and amount, then:

i=0
while read fistname lastname amount; do
    NAMEARRAY[$i]="$firstname $lastname"
    SALARYARRAY[$i]=$amount
    i = `expr $i + 1`
done < "$FILE"

The shell read built-in, automatically splits input. See the variable IFS in the man page for sh(1). If you have data after the amount field, and you wish to ignore it, just create another variable after amount; but don't use it. It will collect everything after the 1st 3 fields into the extra variable.

You specified Bourne shell, so I used some pretty antiquated stuff:

i=`expr $x + 1`

is usually written

let $((i++))   # ksh, bash (POSIX, Solaris, Linux)

On modern systems, /bin/sh is usually ksh or something pretty compatible. You can probably use let $((i++))

Frayser
Thank you both for your help.
Nelson