LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 11-30-2007, 07:41 PM   #1
packets
Member
 
Registered: Oct 2005
Posts: 317

Rep: Reputation: 32
How to display remainder in Bash script


I'm trying to create a script that will convert b/s to kb/s. To convert to kb/s, I must divide it to 1000 b/s. However, the answer does not contain the remainder. When I do "expr 5 / 3", it shows only 1 (suppose to be 1.6). Doesn't show the remainder .6.

I search in Google and it says the "%" is remainder of a division. But when I do expr 5 % 3, it shows "2". It suppose to be 6, am I right?


Any inputs,
 
Old 11-30-2007, 07:51 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
Bash variables are integers only. You could use bc to display it.

meters=14034
echo "scale=2; $meters / 1000" | bc

However, a script that needs to handle floating point numbers probably should be written using another language.

The expression "5 % 3" will give you 1 Remainder 3. The "%" operator is the mod operator.

Last edited by jschiwal; 12-01-2007 at 08:45 AM. Reason: spelling fix.
 
Old 11-30-2007, 08:47 PM   #3
packets
Member
 
Registered: Oct 2005
Posts: 317

Original Poster
Rep: Reputation: 32
When I'm doing this, it has an error

meters=14034
date=`date +%F\ %k:%M`
echo "scale=2; $date $meters / 1000" | bc

Can I put a command before $metes?
 
Old 11-30-2007, 09:14 PM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
The date command doesn't belong there. You want to echo "bc" expressions into the command.

You could separate the two if you wanted the date displayed as well.

Code:
meters=14034
date
echo "scale=2; $meters / 1000" | bc
 
Old 11-30-2007, 10:36 PM   #5
packets
Member
 
Registered: Oct 2005
Posts: 317

Original Poster
Rep: Reputation: 32
Is there a way I can put a variable next to the echo with bc?

Quote:
echo "scale=2; $meters / 1000" | bc

I need to attached a numbers to the output. When I do 'echo -n "scale=2; $meters / 1000" | bc'
it says: (standard_in) 1: parse error
 
Old 12-01-2007, 01:03 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
The echo is not do display something to the screen. It is to send a line to "bc" that the bc shell understands. You could do something similar as you did before but use the -n option to suppress a newline at the end of the echo:
meters=14034
date=`date +%F\ %k:%M`
echo -n "$date "
echo "scale=2; $meters / 1000" | bc
 
Old 12-01-2007, 01:30 AM   #7
4c00h
LQ Newbie
 
Registered: Dec 2007
Distribution: Fedora 8 under i386
Posts: 10

Rep: Reputation: 0
Should be 1024 instead of 1000 if you want exact results
 
  


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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
Bash script to create bash script jag7720 Programming 10 09-10-2007 07:01 PM
[bash] having trouble debugging this bash script. jons Programming 4 02-08-2007 06:51 AM
xset is unable to open display from within bash script copolii Linux - General 3 10-22-2005 06:26 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM


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