LinuxQuestions.org
Support LQ: Use code LQCO20 and save 20% on CrossOver Office
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
 
LinkBack Search this Thread
Old 01-09-2007, 04:37 PM   #1
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 505

Rep: Reputation: 33
Question BASH, simple arithmetic


hello all,
I'm trying to do a calculation of the following all simple in itself but with the inclusion of something like significant figures. eg:

i want to do something like 10-1 = 09 and not 9
what i do at the moment is
x=10

y=$(($x-1))

that comes to 9 but i'm looking for 09 as the answer. and i know if you can set sig fig's to 2. then i should get the answer 09. The only other way i can think of doing it would be to read in the variable "x" count the number of characters in the variable and if its only one character add a 0 at the begining as i'm only using numbers from 01-99

Any ideas welcome thanks a ton
Markles
 
Old 01-09-2007, 04:46 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 9,002

Rep: Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349
You can add 100 to the number and then get only the last two characters, e.g.

Code:
y=`expr $x - 1 + 100 | cut -c 2-3`
This is the way I do it in /bin/sh. I think there is a better way to write this piece of code in /bin/bash.
 
Old 01-09-2007, 04:51 PM   #3
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 505

Original Poster
Rep: Reputation: 33
wow LoL what a simple and yet brilliant idea hehe, now i wish i'd thought of that. Thanks dude thats cool. Yeah i can write that fine in bash No Probs. But if there is a tool that does it properly if anyone knows then let me know cos i'll still be interested. But thanks to that person above
 
Old 01-09-2007, 04:55 PM   #4
raskin
Senior Member
 
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,891

Rep: Reputation: 68
printf "%02d\n" $(( 10 - 1 ))
printf "%02d\n" $(( $x - 1 ))
 
Old 01-09-2007, 04:58 PM   #5
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 505

Original Poster
Rep: Reputation: 33
cheers again hit the target right on the noggin. Thanks again i dont know what that thing is "printf" but sure am going to look into it looks like i might be able to use it in some other places as well so thanks very much peeps

see told you it was simple. but its only simple to those who are in the know
 
Old 01-09-2007, 05:17 PM   #6
raskin
Senior Member
 
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,891

Rep: Reputation: 68
Well, glance through POSIX. Glance through GNU coreutils list (even better - POSIX is theory, coreutils are practice). Until you do it you miss a lot when bash scripting.
 
Old 01-09-2007, 06:12 PM   #7
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 505

Original Poster
Rep: Reputation: 33
thanks but erm as bad as it might be to say this, what is posix and the other thing, i'm guessing some sort of documentation or "man" entries?? sorry am near to being a newb after all.
 
Old 01-10-2007, 12:31 AM   #8
raskin
Senior Member
 
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,891

Rep: Reputation: 68
info coreutils
will do...

Being nearly newbie while aiming to learn is nothing to be sorry for. If you never used 'info', it's hard to guess...

http://www.opengroup.org/onlinepubs/009695399/toc.htm
POSIX standard (one of revisions). Found by Snap for word POSIX.
 
Old 01-10-2007, 05:13 PM   #9
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 505

Original Poster
Rep: Reputation: 33
thanks i will be looking at it
 
Old 01-10-2007, 05:53 PM   #10
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.2, Centos 5.8
Posts: 11,740

Rep: Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905
Well, if you haven't already, I highly recommend you bookmark & read these 2 links:
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/abs/html/
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash arithmetic Blackout_08 Programming 2 06-08-2006 10:37 PM
simple arithmetic in bash gfrair Linux - Newbie 9 03-16-2005 02:09 PM
Bash, non-integers and arithmetic causticmtl Programming 5 07-16-2003 09:15 AM
Basic Java arithmetic question chr15t0 Programming 2 07-05-2003 01:32 PM
arithmetic operators in Kylix3 (C++) herbie_52 Programming 2 05-23-2003 07:36 AM


All times are GMT -5. The time now is 11:17 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration