LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 08-31-2010, 11:17 AM   #1
HarryBoy
Member
 
Registered: Apr 2008
Distribution: MontaVista Linux Version 4.0.1, Professional Edition
Posts: 215

Rep: Reputation: 16
Bash script question - formatting a number


Hi,

I'm new to bash scripting,I am doing the following:

echo 'hi'
my_new_no=($((my_old_no))*2.2)
echo $my_new_no

but the output is:
hi
1600028*2.2

I want this to be a 'value' (the result of my calculation)and not a string. How do I do this?

Thanks
 
Old 08-31-2010, 11:26 AM   #2
alunduil
Member
 
Registered: Feb 2005
Location: San Antonio, TX
Distribution: Gentoo
Posts: 684

Rep: Reputation: 62
I don't remember which utility it is (bc maybe?) but bash by itself is unable to do floating point operations. When I try to run the simple math command: `echo $(( 160028*2.2 ))` I get the following output:

Code:
bash: 160028*2.2 : syntax error: invalid arithmetic operator (error token is ".2 ")
Perhaps perl or awk would be easier to do math with?

Regards,

Alunduil
 
Old 08-31-2010, 11:28 AM   #3
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Quote:
Originally Posted by HarryBoy View Post
Hi,

I'm new to bash scripting,I am doing the following:

echo 'hi'
my_new_no=($((my_old_no))*2.2)
echo $my_new_no

but the output is:
hi
1600028*2.2

I want this to be a 'value' (the result of my calculation)and not a string. How do I do this?

Thanks
I THINK that you can only do whole numbers...1, 2, 3 (someone correct me if I'm wrong)

You can use "bc" like so...

Code:
#!/bin/bash
echo 'hi'
my_old_no=3
my_new_no=$( echo "${my_old_no}*2.2" | bc -l)
echo $my_new_no
 
Old 08-31-2010, 11:44 AM   #4
HarryBoy
Member
 
Registered: Apr 2008
Distribution: MontaVista Linux Version 4.0.1, Professional Edition
Posts: 215

Original Poster
Rep: Reputation: 16
thanks

Code:
newno=$( echo "${oldno}*2.2" | bc -1)
gives me:

-bash: bc: command not found

???

Thanks
 
Old 08-31-2010, 11:51 AM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Try replacing the my_new_no=($((my_old_no))*2.2) by my_new_no=$((${my_old_no}*2.2))

Last edited by PTrenholme; 09-01-2010 at 12:11 PM. Reason: Typo + brain fart
 
Old 08-31-2010, 12:02 PM   #6
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Quote:
Originally Posted by HarryBoy View Post
Code:
newno=$( echo "${oldno}*2.2" | bc -1)
gives me:

-bash: bc: command not found

???

Thanks
Well it depends on what version on Linux you are using...bc should be standard...try /usr/bin/bc
 
Old 08-31-2010, 12:36 PM   #7
valen_tino
Member
 
Registered: Jan 2008
Posts: 105

Rep: Reputation: 28
@HarryBoy

You should be typing bc -l and not bc -1
 
Old 08-31-2010, 01:45 PM   #8
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 133Reputation: 133
declare -i makes a variable an integer.

declare -i my_old_no=...
declare -i my_new_no=$((my_old_no*2.2))

Note: without "declare -i", the calculation will still produce the proper result. That result will be a string but it will look as if it were numerical.

Last edited by jay73; 08-31-2010 at 02:22 PM.
 
Old 08-31-2010, 02:20 PM   #9
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
If bc is not available, an alternative option to do floating point arithmetic is awk:
Code:
my_new_no=$(awk -v i=$my_old_no 'BEGIN{print i*2.2}')

Last edited by colucix; 08-31-2010 at 02:22 PM.
 
Old 08-31-2010, 10:02 PM   #10
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
here's another one:
Code:
[schneidz@hyper ~]$ a=5
[schneidz@hyper ~]$ b=7.5
[schneidz@hyper ~]$ echo $a $b | awk '{c = $1 * $2} END {print " c = " c}'
 c = 37.5
 
Old 08-31-2010, 10:07 PM   #11
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,624

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
edit

Last edited by John VV; 08-31-2010 at 10:10 PM.
 
  


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: Help with formatting Output from a script SilversleevesX Programming 4 08-23-2010 06:59 PM
Bash Script for Moving X number files from /direct1 to /direct2? Supreme1012 Programming 14 01-30-2010 05:08 PM
Bash script to run commands for specified number of seconds? devmoc Linux - Software 14 12-07-2009 06:19 PM
Bash script to change a filename associated with an inode index number. Ziv Programming 22 06-19-2004 08:41 AM
bash script to download a number of things linksocc Linux - Software 3 12-10-2003 12:18 PM

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

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