DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1   (View Single Post)  
Old 26th July 2019
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,131
Default sh script to convert inches to mm and cm

I found a few interesting DIY wood working projects on the interwebs and because the measurements were in inches I wrote a simple sh script to convert these to millimeters and centimeters.

Code:
#!/bin/sh

# use 'bc(1)', the multiprecision calculator to convert inches to mm and cm

INCH='25.4'     # millimeter (mm)
PRECISION=4     # nr of fractional digits

VALUE=1

cat <<END
You can enter values like '1 1/4' or '2 3/8' inch as '1+1/4' and '2+3/8'
-----------------------
Nr of inches: 1/8
1/8 inch = 3.1750 mm or .3175 cm

Nr of inches: 1+1/4
1+1/4 inch = 31.7500 mm or 3.1750 cm
-----------------------

Press CNTRL-C to exit.

END

while true ; do    
        printf "\nNr of inches: " 
        read VALUE
        MM=$( echo "scale = ${PRECISION} ; ( ${VALUE} ) * ${INCH}" | bc )
        CM=$( echo "scale = ${PRECISION} ; ${MM} / 10" | bc )
        echo "${VALUE} inch = ${MM} mm or ${CM} cm"
done
Example where I convert the size of a 2 by 4" and the 1/16" holes to drill in a 3/4" PVC irrigation pipe:

Code:
$ ./inch2milli.sh
You can enter values like '1 1/4' or '2 3/8' inch as '1+1/4' and '2+3/8'
-----------------------
Nr of inches: 1/8
1/8 inch = 3.1750 mm or .3175 cm

Nr of inches: 1+1/4
1+1/4 inch = 31.7500 mm or 3.1750 cm
-----------------------

Press CNTRL-C to exit.


Nr of inches: 2
2 inch = 50.8 mm or 5.0800 cm

Nr of inches: 4
4 inch = 101.6 mm or 10.1600 cm

Nr of inches: 1/16
1/16 inch = 1.5875 mm or .1587 cm

Nr of inches: 3/4
3/4 inch = 19.0500 mm or 1.9050 cm

Nr of inches: ^C$
Attached Files
File Type: sh inch2milli.sh (683 Bytes, 56 views)
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
 


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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with convert aleunix OpenBSD Packages and Ports 2 10th May 2012 01:52 PM
How to convert "no-nat" to new OpenBSD 4.7 NAT/RDR syntax J65nko Guides 0 3rd April 2011 12:14 AM
How to convert MD5 hash into shadow format? guitarscn General software and network 2 29th November 2010 12:54 PM
Want to convert my server to raid revzalot OpenBSD Installation and Upgrading 2 16th September 2009 07:56 PM
Convert varchar () to datetime datatype in MYSQL cksraj Programming 1 22nd June 2009 12:53 PM


All times are GMT. The time now is 06:46 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick