LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-05-2012, 07:30 PM   #1
maneshmotts
LQ Newbie
 
Registered: Sep 2011
Posts: 16

Rep: Reputation: Disabled
Question Help need for shell script


HI ,

I need help regarding the shell scripting . I used the following commands on Centos flavoured linux

Code:
x=1000
y=1
expr x / y
when is user expr it showed the following error
Quote:
"expr: non-numeric argument"
I needed your guys help in fix this error.

Thanks.
 
Old 04-05-2012, 08:09 PM   #2
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Try expr ${x} / ${y} . Without the $ "x" means a literal x, the $x (or, pedantically, ${x}) means "the value of the variable x.

Here's an example:
Code:
$  x=1000;y=1;expr ${x} / ${y}; expr ${y} / ${x}
1000
0
Note that the value of y/x = 0 is the correct answer. That's because bash arithmetic is "integer values only."

By the way, the expr command is not really necessary. Consider this:
Code:
$ x=1000;y=1;echo $((${x} / ${y}));echo $((${y} / ${x}))
1000
0
Or this one:
Code:
$ x=1000;y=1;echo "scale=3;${x} / ${y}" | bc;echo "scale=3;${y} / ${x}" | bc
1000.000
.001
See info bash, info expr and info bc for details.

Last edited by PTrenholme; 04-05-2012 at 08:11 PM.
 
Old 04-06-2012, 01:53 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
This page should give you all you need to know about doing integer arithmetic in the shell:

http://mywiki.wooledge.org/ArithmeticExpression

There's also a link on that page to another one describing how to do floating-point math. Floating point nearly always requires an external tool.

And as pointed out, expr is pretty much completely superfluous these days. It's probably best to just forget that it exists at all.

(Actually, it does have one or two other features that can still come in handy, but only on very rare occasions. You certainly don't need it for doing math.)
 
Old 04-06-2012, 02:23 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
I thought I would add to the following example by David:
Quote:
$ x=1000;y=1;echo $((${x} / ${y}));echo $((${y} / ${x}))
And say that when using the round brackets the dollar symbols for variables is also not required:
Code:
$ x=1000;y=1;echo $(( x / y ));echo $(( y / x ))
1000
0
 
Old 04-06-2012, 02:38 PM   #5
maneshmotts
LQ Newbie
 
Registered: Sep 2011
Posts: 16

Original Poster
Rep: Reputation: Disabled
Thanks guys it help me a lot . Thank you so much !!!
 
Old 04-07-2012, 04:53 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
Please mark as SOLVED once you have a solution
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 10:12 PM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 08:58 PM
Shell script calling shell script - List of all nikunjbadjatya Programming 7 04-13-2011 07:27 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM


All times are GMT -5. The time now is 04:31 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration