0

I'm just trying to do some simple math that is not turning out that simple. this is what I have tried so far and I have yet to find anything on the google net that combines integer and floating numbers adding or subtracting as I will be doing both --

I am needing to keep a running total MB processed, and total MB saved - so both adding and subtracting both float and integer together and subtracting them together is needed to do.

#! /bin/bash


# I've tried with and without using this typeset to integer
# even though float won't work with integer -- just ball parking 
# and googling - :\

# typeset -i MB1 MB2


# using EXIFTOOL to get the megabytes off of files then add and subtract
# the values is what I need to do


FileSize1="`exiftool '-File Size'  "The Motels - Careful.mp3" -p   '$FileSize'`"


MB1="${FileSize1% *}" # removes the MB and leaves just the numbers both
                      # integer and float with demimal point - 3.3 



FileSize2="`exiftool '-File Size'  "02 Only The Lonely.flac" -p '$FileSize'`"

MB2="${FileSize2% *}"

echo "$FileSize1"
echo "$MB1"
echo "$FileSize2"
echo "$MB2"

6.4 MB
6.4
19 MB
19

total=`echo $MB1 + $MB2 | bc`  

echo $total  "   total"

# error message here is: 
# ./getMB: line 20: 6.4+19: syntax error: invalid arithmetic operator (error token is ".4+19")

answer=$(($MB1+$MB2)) # doesn't work

echo "$answer" " -- answer=="

# error message : line 16: 6.4+19: syntax error: invalid arithmetic operator (error token is ".4+19")

answer=`expr $MB1 + MB2`

echo "$answer" " -- answer=="

# error message : expr: non-integer argument

# then added typeset -i MB1 MB2
# then I get this error message
# ./getMB: line 7: ���������
# : 6.4: syntax error: invalid arithmetic operator (error token is ".4")

echo "$answer" " -- answer=="

  • 1
    Neither bash nor expr can do floating point arithmetic. You'll have to use a calculator like bc or a different programming language, like awk. – glenn jackman Nov 8 '14 at 0:56
  • @glennjackman thanks I got it figured out now, thanks -- I posted my answer -- – uxserx-bw Nov 9 '14 at 21:12
0

using bc to do float and integer basic math keeping a running tally in a loop

#!/bin/bash

#variable to keep changing amount
#declared outside of loop before it
#runs

changeVal=0



while amount < stopPoint ; do

# get the MB of orginal file
FileSize1="`exiftool '-File Size'  "$FILENAME" -p '$FileSize'`"

#re-sampling mp3 code here 
 lame what ever args firstFileName endFileName

# get the MB of new file
FileSize2="`exiftool '-File Size'  "$FILENAME" -p '$FileSize'`"

# strip off the 'MB' leaving just the values
MB1="${FileSize1% *}"
MB2="${FileSize2% *}"


# out put formatted by using spaces 
echo "  "$MB1"  MB1 - start size"
echo "- "$MB2"  MB2 - ending size"
#holds remaining value 
totalSaveOnFile=`echo $MB1 - $MB2 | bc`
echo "----------"
echo "  "$totalSaveOnFile" regained space" 
 #keeps last total -- then adds it to a new remaining value
 # giving a new tally of total 
 maxSaved=`echo $totalSaveOnFile + $maxSaved | bc`

 echo "  "$maxSaved " Total space saved do far"

 let amount++
 done 

the loop is not runnable -- but the code to keep a running tally is good code -- tested it myself after I figured out how to use bc thanks to @glenn jackman pointing me to it.

maxSaved=`echo $totalSaveOnFile + $maxSaved | bc`

totalSaveOnFile holds the new total of the difference between the old file and re-sampled file sizes, then is added to the maxSaved by adding what is already inside of the variable to itself and the new totalSaveOnFile variable to give an new over all total of amount of space saved on HDD. using both floating point and integer values together.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.