Register
It is currently Wed Aug 20, 2014 12:49 am

enhanced bfcalc, called carpcalc


All times are UTC - 6 hours


Post new topic Reply to topic  [ 1 post ] 
Author Message
 PostPosted: Tue May 17, 2005 8:21 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 666
Location: Des Moines, Iowa
Author mekanik

after seeing the bfcalc script, orginally coded by crouse, i decided to whip up an enchanced version using only cli args as the input...below is the result...

Code:
#!/bin/bash
: 'begin'
## set -x                                            ## used for debugging purposes only
##
## @(#) carpcalc.sh              this code is an enhancement to <crouse's> original code
##                               which was written to calculate 'board footage' for some
##                               lumberyards or carpenters.
##
## programmer: mekanik
## revision:   1.0               program development enhancement to "bfcalc".  with this
##                               enchancement, i have added variable checking along with
##                               it being all arguement driven.
## date:       20050202
##
## this program has been originally written by [mekanik [root23 NOS<- AT ->PAM gmail.com]
##
## -------------------------------------------------------------------------------------
##  This software is licensed under the GPL - GNU General Public License
## -------------------------------------------------------------------------------------
##
## my disclaimer:
##   first of all, i am _NOT_ responsible for anything bad that might happen because
##   of this program...it doesn't do anything bad on my system, but it is not my fault
##   if it does something bad on your system...
##


## -------------------------------------------------------------------------------------
## global vars and functions
## -------------------------------------------------------------------------------------
_version="1.0"
_thick=$2
_width=$4
_length=$6


_usage_short() {
## -------------------------------------------------------------------------------------
## function to display the short help usage for the program
##
cat <<- _usage_
   usage:   `basename $0`  [-t thickness] [-w width] [-l length] [-h|-help] [-v|-version]
_usage_
}

_usage_long() {
## -------------------------------------------------------------------------------------
## function to display the long help usage for the program
##
cat <<- _usage_
        usage:   `basename $0`  [-t thickness] [-w width] [-l length] [-h|-help]

        -t   =  thickness of the wood in inches
        -w   =  width of the wood in inches
        -l   =  length of the wood in inches

        -h   =  display the verbose help usage
        -v   =  display the program version

_usage_
}


## -------------------------------------------------------------------------------------
## main program
##
if   [ $# = 0 ];then
     _usage_short
     exit 99
elif [ X$1 = X"-h"  -o  X$1 = X"-help" ] && [ $# = 1 ];then
     _usage_long
     exit 98
elif [ X$1 = X"-v"  -o  X$1 = X"-version" ] && [ $# = 1 ];then
     printf "program '`basename $0`' version:  $_version\n"
     exit 97
elif [ $# != 6 ];then
     _usage_short
     exit 96
elif [ $# = 6 ];then
     if   [ X$1 != X"-t" ];then
          _usage_short
          exit 95
     elif [ X$3 != X"-w" ];then
          _usage_short
          exit 94
     elif [ X$5 != X"-l" ];then
          _usage_short
          exit 93
     fi
fi


## reject if argument:  contains any character other than digits or period,
##                      begins with a period, ends with a period, and/or it
##                      contains 2 (or more) adjacent periods
##
## go ahead and strip off whitespace, backslash, forwardslash, blah blah blah...
##
_thick=`printf "$_thick" | sed 's/[ /\//]//g'`
_width=`printf "$_width" | sed 's/[ /\//]//g'`
_length=`printf "$_length" | sed 's/[ /\//]//g'`

for _check in $_thick $_width $_length;do
   case "$_check" in
      *[!0-9.]*|.*|*.|*..*|0|*.*.*|0.0)
      printf "one of the variables isn't correct, check an re-run.  exiting\n"
      ##return 1 ;;
      exit 100 ;;
   esac
done

## go ahead and split the args into positional parameters just to verify that
## there is only one element per arguement, i think that this is doing something
## somewhat useful.
##
set -- "${_thick// / }"
set -- "${_width// / }"
set -- "${_length// / }"

_chkt=`printf "${#_thick[@]}\n"`
_chkw=`printf "${#_width[@]}\n"`
_chkl=`printf "${#_length[@]}\n"`

if   [ $_chkt != 1 ];then
     exit 89
elif [ $_chkw != 1 ];then
     exit 88
elif [ $_chkl != 1 ];then
     exit 87
fi


## now that we've got all the args tested and validated, plug them into the
## calulations and we get the output.
##
printf "This board contains: \
`printf "scale = 4; (($_thick * $_width * $_length) / 12)\n"\
| bc -l 2>/dev/null` board feet.\n"

: 'end'



lemme know what ya think... :D

-mekanik


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP