LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-10-2010, 04:53 PM   #1
bashprog
LQ Newbie
 
Registered: Sep 2010
Posts: 13

Rep: Reputation: 0
What am I doing wrong in this bash script ?


Why am I getting "expr: non-numeric argument" here ?

I thought variables are not typed in bash.
If I initialize SIZE and TIME from command line the script works well.
Can you help ?

TIA
Code:
#!/bin/sh


report ()
{

echo $SIZE $TIME
RATE=`expr $1 / $2`
echo $RATE


}


i='1.00000,0.02012'



read SIZE TIME <<< `echo "$i" | cut -d , -f1-2 --output-delimiter=' '`

echo $SIZE $TIME

report $SIZE $TIME
 
Old 10-10-2010, 05:17 PM   #2
bashprog
LQ Newbie
 
Registered: Sep 2010
Posts: 13

Original Poster
Rep: Reputation: 0
I don't want to use bc. What I am really curious to know is why am I getting this error in the first place. I thought variables are not typed in bash, and so I should not be getting this error.
 
Old 10-10-2010, 05:52 PM   #3
bashprog
LQ Newbie
 
Registered: Sep 2010
Posts: 13

Original Poster
Rep: Reputation: 0
I know what am I doing wrong, I am doing floating point arithmetic with expr. Not supported.

Can I do floating point arithmetic without using bc ?
 
Old 10-10-2010, 06:07 PM   #4
j1alu
Member
 
Registered: Apr 2009
Distribution: debian gnu/linux
Posts: 798

Rep: Reputation: Disabled
I think the problem is:
Quote:
Bash does not understand floating point arithmetic. It treats numbers containing a decimal point as strings.
from here:
http://tldp.org/LDP/abs/html/ops.html (Example 8.2)

ups: we posted at the same time.

Last edited by j1alu; 10-10-2010 at 06:09 PM.
 
Old 10-10-2010, 07:19 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Quote:
Can I do floating point arithmetic without using bc ?
Yes. Try awk or any of a dozen other scripting languages.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
(BASH) Works on the command line, hangs as a script -- what's wrong? SilversleevesX Programming 17 08-08-2010 10:19 PM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM
What Is Wrong With This Bash Script File? George2 Programming 24 05-11-2006 08:29 PM
My first BASH script, what's wrong with it? szf2 Linux - Newbie 2 11-12-2003 01:43 PM
Basic BASH script, what's wrong??? Satriani Linux - General 2 06-02-2003 05:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration