-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtax.sh
More file actions
executable file
·97 lines (97 loc) · 2.95 KB
/
tax.sh
File metadata and controls
executable file
·97 lines (97 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/bash -
#title :tax.sh
#description :a tax calculator
#author :Viktor Ahlström
#date :20150627
#version :1.9
#usage :./tax.sh
#notes :
#bash_version :3.2.48(1)-release
#============================================================================
get_tax() {
tax=$1
if [[ $tax =~ ^-?[0-9]+$ ]] ; then echo "OK." ; fi
states=(
AL AK AZ AR
CA CO CT
DE DC
FL
GA
HI
ID IL IN IA
KS KY
LA
ME MD MA MI MN MS MO MT
NE NV NH NJ NM NY NC ND
OH OK OR
PA
RI
SC SD
TN TX
UT
VT VA
WA WV WI WY
)
taxes=(
4 0 5.6 6.5
7.5 2.9 6.35
0 5.75
6
4
4
6 6.25 7 6
6.15 6
4
5.5 6 6.25 6 6.875 7 4.225 0
5.5 6.85 0 7 5.125 4 4.75 5
5.75 4.5 0
6
7
6 4
7 6.25
4.7
6 4.3
6.5 6 5 4
)
for ((i=0; i<=50; i++)) ; do # cycle through all 51 2 letter codes
check=$(echo ${states[$i]}) # set the current check code
if [[ $tax == $check ]] # see if the entered code matches the current check code
then
taxpercent=$(echo ${taxes[$i]}) #set the sales tax percentage
break #break away from the loop to avoid excess checking
fi
done
}
if [[ $# == 2 ]] ; then #see if arguments are present
get_tax $1 $2 #run the function shown above with the args that were present when command was run
subtotal=$2 #set the subtotal to the second command
percent=$(echo "scale=5;$taxpercent * 0.01" | bc) #format the tax percentage to proper value
stax=$(echo "scale=2;$subtotal * $percent" | bc) #multiply the subtotal by the percent and store in a variable for later
grandtotal=$(echo "$stax + $subtotal" | bc) #add the tax figured out in the line before to the subtotal
#number formatting=====================================================================###
#
staxdec=$(echo $stax | sed 's/:*.//') #
if [[ ${#staxdec} == 4 ]] ; then grandtotal=${grandtotal%?} ; fi #
if [[ ${#staxdec} == 5 ]] ; then grandtotal=${grandtotal%??} ; fi #
if [[ ${#staxdec} == 4 ]] ; then stax=${stax%?} ; fi #
if [[ ${#staxdec} == 5 ]] ; then stax=${stax%??} ; fi #
if [[ ${#stax} == 4 ]] ; then grandtotal=${grandtotal%?} ; fi #
if [[ ${#stax} == 5 ]] ; then grandtotal=${grandtotal%??} ; fi #
if [[ ${#stax} == 4 ]] ; then stax=${stax%?} ; fi #
if [[ ${#stax} == 5 ]] ; then stax=${stax%??} ; fi #
if [[ $stax < 1 ]] ; then #
stax=$(echo -ne "0" ; echo -ne $stax) #
else #
stax=$stax #
fi #
#
#======================================================================================###
echo -e "###########################"
echo -e "SUBTOTAL \$$subtotal "
echo -e "SALES TAX % $taxpercent% "
echo -e "SALES TAX \$$stax "
echo "###########################"
echo -e "GRAND TOTAL \$$grandtotal"
else #run on an invalid input
echo "Usage: tax.sh <[state] [tax percent]> <subtotal>"
fi