LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-13-2014, 05:13 PM   #1
Ela1987
LQ Newbie
 
Registered: May 2011
Posts: 3

Rep: Reputation: Disabled
Unix command for modulus


HI,

Am trying to find the modulus using below two commands but i dont understand why the output differs. Can someone help me why there is difference and which one is correct command ?

1) mod=$((`expr ${INPUT_NUMBER} % 4`))
OUTPUT:
echo $mod
0[/COLOR]

2) mod=$(( $(INPUT_NUMBER} % 4 ))
OUTPUT:
echo $mod
2
 
Old 02-14-2014, 10:48 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Those both work, you have a typo in the second one. However it seems as if you're saying that the first doesn't work and the second one does.

Code:
$ INPUT_NUMBER=18
$ MOD=$((`expr ${INPUT_NUMBER} % 4`))
$ echo $MOD
2

$ MOD2=$(( ${INPUT_NUMBER} % 4 ))
$ echo $MOD2
2
 
Old 02-14-2014, 11:15 AM   #3
Ela1987
LQ Newbie
 
Registered: May 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
Sorry it was typo in my 2nd command, Can you try for the number 0272 as input number ? it should give different output.
 
Old 02-14-2014, 11:24 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The problem is that bash treats numbers with leading zeros as octal. You can still use the second command (without expr) but you have to force the shell to treat the number as decimal, e.g.
Code:
MOD2=$(( 10#${INPUT_NUMBER} % 4 ))
 
Old 02-14-2014, 04:44 PM   #5
Ela1987
LQ Newbie
 
Registered: May 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
Excellent answer, This whats i was looking for.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
The modulus operator in UNIX throws random values giving weird output gaynut Programming 5 07-08-2008 12:15 AM
Java Modulus operator freakyg Programming 3 06-06-2007 10:06 PM
bc: using -l messes up modulus Ephracis Programming 4 06-07-2005 01:34 PM
Divisibility without using modulus redhatrosh Programming 1 03-01-2005 10:02 PM
Modulus in Assembly How? browneyes Programming 2 10-07-2004 12:42 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:40 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