Floating point operations with bash

how can i convert the string "620/100" to "6.2" in a bash script

The context of my question is about image processing. EXIF data encodes the focal length in fractional format, while I need the corresponding decimal string.

Thanks for the help, Olivier

+6
source share
3 answers

Use bc -l

 bc -l <<< "scale=2; 620/100" 6.20 

OR awk:

 awk 'BEGIN{printf "%.2f\n", (620/100)}' 6.20 
+11
source

bash does not support floating point.

You can use bc :

 $ echo "50/10" | bc -l 5.00000000000000000000 $ echo "scale=1; 50/10" | bc -l 5.0 
+3
source

Thanks for answers. bc was what i needed.

I do not know if publishing the result can use. In any case, this is the last piece of code for the emergency focal length of a photograph and print in decimal format. It should work for all cameras. Tested on 4 cameras of 3 different brands.

 F="your_image.JPG" EXIF=$(exiv2 -pv "$F") FocalFractional=$( echo "$EXIF" | grep -E '[^ ]* *Photo *FocalLength '| grep -iohE "[^ ]* *$" ) Formula="scale=2; "$FocalFractional FocalDecimal=$( bc -l <<< "$Formula" ) echo $ FocalDecimal 
+2
source

All Articles