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 01-13-2017, 02:57 AM   #1
arun natarajan
Member
 
Registered: Jun 2014
Posts: 111

Rep: Reputation: Disabled
arithmetic in shell script


Hi,

Though its a simple question, am unable to find answer for this.
Expecting the value of "e" as 7.

[root@localhost ~]# cat 1.sh

a=1

b=2

c=`expr $a + $b`

d=4

e=`expr $c + $b`

echo $e
[root@localhost ~]# sh 1.sh
5
[root@localhost ~]#
 
Old 01-13-2017, 03:19 AM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,292
Blog Entries: 24

Rep: Reputation: 4251Reputation: 4251Reputation: 4251Reputation: 4251Reputation: 4251Reputation: 4251Reputation: 4251Reputation: 4251Reputation: 4251Reputation: 4251Reputation: 4251
Why would you expect 3 + 2 to equal 7?
 
Old 01-13-2017, 03:20 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,614

Rep: Reputation: 7524Reputation: 7524Reputation: 7524Reputation: 7524Reputation: 7524Reputation: 7524Reputation: 7524Reputation: 7524Reputation: 7524Reputation: 7524Reputation: 7524
do not use root for such a simple tests
Code:
# use 
c=$(expr $a + $b)
# instead of backticks, or even better: use 
c=$(( a + b ))
last but not least why do you think it should be 7? I think 5 is the correct result. Probably you mistyped: e=c+b
 
Old 01-13-2017, 07:15 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,891
Blog Entries: 13

Rep: Reputation: 4944Reputation: 4944Reputation: 4944Reputation: 4944Reputation: 4944Reputation: 4944Reputation: 4944Reputation: 4944Reputation: 4944Reputation: 4944Reputation: 4944
As stated by pan64 and astrogeek, do not use root. Another thing to do is turn on verbose output by adding a "set -xv" line near the top of your script. Also you should add a shebang line at the top of it:
Code:
#!/bin/bash

set -xv // turn on verbose debugging, comment out to remove

a=1
b=2
c=`expr $a + $b`
# d=4 commented out since it is irrelevant, however as other say, you've probably committed a typo.
e=`expr $c + $b` // This probably should be $d, correct?
echo $e
Note that for when you start to explore different forms of math, they will not work in this way, consider the bc command as well as the -l option if you wish to start performing floating point computations.
 
Old 01-13-2017, 09:33 AM   #5
arun natarajan
Member
 
Registered: Jun 2014
Posts: 111

Original Poster
Rep: Reputation: Disabled
Thanks guys.

I learnt new thing to use set -xv from you people from this post.
 
Old 01-13-2017, 10:27 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,017

Rep: Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196
Further to examples by pan64, I like to use:
Code:
(( c = a + b ))
 
Old 01-13-2017, 10:56 AM   #7
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Arun, Did you also learn not to use root for such tasks? Or do you always login as root to avoid any permission problems?

OK
 
  


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
LXer: Shell Scripting Part 2: Accepting Inputs and Performing Shell Arithmetic LXer Syndicated Linux News 0 04-30-2015 07:10 AM
Arithmetic operations in a shell script towheedm Programming 7 03-27-2012 09:29 AM
Arithmetic in shell scripts Kenny_Strawn Programming 14 01-02-2011 09:18 PM
[SOLVED] bash shell script arithmetic mindcontrolusa Programming 6 11-10-2010 01:02 PM
Do shell scripts permit arithmetic operations? johnpaulodonnell Linux - Newbie 6 01-30-2007 10:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:16 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
Open Source Consulting | Domain Registration