Expr problem and other basic problems

Hello, I am new to the Bash scripting language, and was given a tutorial page on how to setup a file. However I am trying to use cygwin to run this file and it is not working.

$ vi averagetime.sh
#
#
#
echo "Enter Dictorinary File Text "
read dict
echo "Enter Grid Name"
read grid
RUNNING_TIME=`./a.exe $dict $grid | tail -1`
RUNNING_TIME1=`./a.exe $dict $grid | tail -1`
RUNNING_TIME2=`./a.exe $dict $grid | tail -1`
RUNNING_TIME3=`./a.exe $dict $grid | tail -1`
RUNNING_TIME4=`./a.exe $dict $grid | tail -1`
AVERAGETIME=`expr ($($RUNNING_TIME + $RUNNING_TIME1 + $RUNNING_TIME2 + $RUNNING_TIME3 + $RUNNING_TIME4)/5)`
echo "Average time for 5 runs is: $AVERAGETIME"
exit 0

This was the output from the run:

./averagetime.sh: line 1: $: command not found
Enter Dictorinary File Text
./averagetime.sh: line 6: read: `dict
': not a valid identifier
Enter Grid Name
./averagetime.sh: line 8: read: `grid
': not a valid identifier
./averagetime.sh: command substitution: line 14: syntax error near unexpected token `$($RUNNING_TIME + $RUNNING_TIME1 + $RUNNING_TIME2 + $RUNNING_TIME3 + $RUNNING_TIME4)/5'
./averagetime.sh: command substitution: line 14: `expr ($($RUNNING_TIME + $RUNNING_TIME1 + $RUNNING_TIME2 + $RUNNING_TIME3 + $RUNNING_TIME4)/5)'
Average time for 5 runs is:

Hi.

This part is wrong:

 ($($RUNNING_TIME

It means you are trying to execute (run) something called RUNNING_TIME.

The bracket ( should be before the $ sign. And I don't think the outer-most brackets are required for this calculation.

Also, this kind of arithmetic can be done using the shell, and not external commands like expr.

There's no need for expr; use the shell:

AVERAGETIME=$(( ($RUNNING_TIME + $RUNNING_TIME1 + $RUNNING_TIME2 + $RUNNING_TIME3 + $RUNNING_TIME4)/5 ))

what does the not a valid identifier indicate and how do I go about fixing this?

It means you are trying to assign a value to a word that is not a valid variable name.

See my previous post.

Sorry, should have clarified, I meant on the two read variables it gives me not a valid identifier

What shell are you using?

and if I may add...

Stop using backticks ` ! (unless you're using CSH, in which case stop using CSH!)

Use $( .... )

i.e. this=$(that)
not this=`that`

It's more standard, and a whole lot more readable.

Did you write the script on a Windows box? If so, remove the carriage return from the end of each line.

bourne shell

The backticks are no less standard than $( ... ).
See Shell Command Language

yes I am using cygwin so it is windows, what is the command to eliminate the \r

tr -d '\r' < FILENAME > NEWFILENAME

Or:

sed 's/\r$//' FILENAME > NEWFILENAME

Better would be to use a text editor that allows you to save the file without the CRs.

Sorry, I use AIX, so I generally believe what's relevant for me, not what may or may not be "standard of the week".

Help -

The standard is (as I have pointed out before) defined by the Open Group. It is not a "standard of the week". It was updated last year; the previous update was a decade or so earlier.

When talking about a standard, it helps to refer to the document that defines that standard, not to something that may or may not be correct.