Visit Our UNIX and Linux User Community


Expr problem and other basic problems


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Expr problem and other basic problems
# 1  
Old 10-21-2009
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.
Code:
$ 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:
# 2  
Old 10-21-2009
Hi.

This part is wrong:

Code:

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

Last edited by Scott; 10-21-2009 at 03:44 PM..
# 3  
Old 10-21-2009
Quote:
Originally Posted by killerqb
Code:
AVERAGETIME=`expr ($($RUNNING_TIME + $RUNNING_TIME1 + $RUNNING_TIME2 + $RUNNING_TIME3 + $RUNNING_TIME4)/5)`


There's no need for expr; use the shell:
Code:
AVERAGETIME=$(( ($RUNNING_TIME + $RUNNING_TIME1 + $RUNNING_TIME2 + $RUNNING_TIME3 + $RUNNING_TIME4)/5 ))

# 4  
Old 10-21-2009
what does the not a valid identifier indicate and how do I go about fixing this?
# 5  
Old 10-21-2009
Quote:
Originally Posted by killerqb
what does the not a valid identifier indicate

It means you are trying to assign a value to a word that is not a valid variable name.
Quote:
and how do I go about fixing this?

See my previous post.
# 6  
Old 10-21-2009
Sorry, should have clarified, I meant on the two read variables it gives me not a valid identifier
# 7  
Old 10-21-2009
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.

Previous Thread | Next Thread
Test Your Knowledge in Computers #299
Difficulty: Easy
Novell bought the SUSE (then SuSE) brands and trademarks in 2003.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with expr command in shell script

Hi, I have used expr command to increment the date. for e.g., case 1 : echo $(date -d $(echo `expr 20010101 + 1`)) it returns Tue Jan 2 00:00:00 IST 2001 case 2: echo $(date -d $(echo `expr 20010101 - 1`)) it returns date: invalid date `20010100' please suggest me, how to... (3 Replies)
Discussion started by: nanthagopal
3 Replies

2. UNIX for Dummies Questions & Answers

Basic if statement problem

I am using the following code: if ; then I am getting an error saying line 12: The code is supposed to examine whether the filename read into $FILE includes the string IMG*. I do have a fi later in the script, but I must be missing something obvious. Can anyone help? (3 Replies)
Discussion started by: Bengel
3 Replies

3. Programming

Problems using Perl DBI to edit database entries - basic stuff

Hello. I am taking a Perl class in college and we've briefly covered SQL and moved on. We have a term project and we can do whatever we want. My project will rely strongly on an SQL Database so I am trying to learn as much about Perl DBI as I can to get things up and going. I am basically... (1 Reply)
Discussion started by: Dave247
1 Replies

4. Shell Programming and Scripting

Expr strange problem to me

Hi all, Please help me solve below issue. expr 04170000000 + 1 gives me -124967295 and offcourse I want this to be 04170000001 and it happens for some sort of number like some other 02300000000 02600000000 03800000000 I guess after exceeding certain range it is converting it somewhere... (2 Replies)
Discussion started by: Revansing
2 Replies

5. Shell Programming and Scripting

Basic problem

Hello Friends, I am learning Perl now. I have a small query. I have a directory Z with file name Z.txt. I would like to copy this file Z.txt to 3 new dir with new filenames as follows dir 1 1.txt dir 2 2.txt dir 3 3.txt I would like to then open 1.txt from dir 1 and edit the first... (0 Replies)
Discussion started by: ramesh54
0 Replies

6. UNIX for Dummies Questions & Answers

problem with expr command

:) hi Unix gurus, Pls consider the following piece of code str='hello' length=echo $str|wc -c echo $length y= ` expr \( 80 - $length \) ` echo $y :confused: The last echo stmt is displaying 0 as the result. If i put direct value like 6 instead of $length in i 3rd stmt it is giving... (8 Replies)
Discussion started by: ravi raj kumar
8 Replies

7. UNIX for Dummies Questions & Answers

expr problem

Hi, in my ksh script expr 22 / 10 results as 2 but the actual result expected in 2.2. how do i get that result. Please help Thanks, (4 Replies)
Discussion started by: kotasateesh
4 Replies

8. Shell Programming and Scripting

expr problem

Hi, in my ksh script expr 22 / 10 results as 2 but the actual result expected in 2.2. how do i get that result. Please help Thanks, (2 Replies)
Discussion started by: kotasateesh
2 Replies

9. UNIX for Dummies Questions & Answers

Problems with expr

I'm writing a unix script that simply does some basic command line stuff for a whole lot of similar directories (ls *.dat > foo.bar). Its my first script ever, and its going ok, except I can't, for the life of me, figure out how to get arithmetic to work in the script. FIVE=5 MY_NUMBER='expr... (4 Replies)
Discussion started by: benu302000
4 Replies

10. UNIX for Dummies Questions & Answers

Problems with expr command

Hi All, I might be making a silly mistake but I need ur help. I have initialized various variables: cur_month=`date +%m` cur_year=`date +%y` last_year=`expr $cur_year \- 1` It works fine for cur_month & cur_year....but has problems with last_year. FOR LAST YEAR IT GIVES A FOLLOWING... (2 Replies)
Discussion started by: rooh
2 Replies

Featured Tech Videos