1#!/bin/bash
2 
3# get weekly pay (written by Chris and Copilot) - math with bc
4# use options flags to take in command line arguments for hours and rate
5 
6print_usage() {
7 echo "Usage: $0 [-h] [-r rate] [-t hours]"
8 echo " -h: print usage"
9 echo " -r: hourly rate"
10 echo " -t: hours worked"
11}
12 
13taxes() {
14 tax_rate=0.15
15 echo "$pay"
16 tax_pay=$(echo "scale=2; $pay * (1-$tax_rate)" | bc)
17}
18# define default values for hours and rate (empty string)
19hours=""
20rate=""
21 
22# list of arguments allowed in the input
23# optstring followed by colon means that the option requires an argument
24optstring="hr:t:"
25 
26# if [ $# -eq 0 ]; then
27# fi
28 
29# getopts is a bash builtin function that parses command line arguments
30# valid arguments are in optstring (above) (e.g. -h, -r, -t)
31# if an invalid argument is passed, while loop will exit and print usage
32while getopts $optstring opt; do
33 case $opt in
34 h)
35 print_usage
36 exit 0
37 ;;
38 r)vs
39 rate=$OPTARG
40 ;;
41 t)
42 hours=$OPTARG
43 ;;
44 \?)
45 print_usage
46 exit 1
47 ;;
48 esac
49done
50 
51# while loop to check if the arguments are empty
52while [ -z "$hours" ] || [ -z "$rate" ]; do
53 
54 # if the arguments are empty, prompt the user for input
55 if [ -z $hours ]; then
56 echo "hours is required"
57 read -p "enter hours worked: " hours
58 fi
59 
60 if [ -z $rate ]; then
61 echo "rate is required"
62 read -p "enter hourly rate: " rate
63 fi
64done
65 
66echo "hours: $hours"
67echo "rate: $rate"
68 
69# calculate the weekly pay using bc
70# using bc because it is a command line calculator and can handle decimals
71# bash builtin can't handle decimals
72pay=$(echo "scale=2; $hours * $rate" | bc)
73 
74taxes
75 
76echo "weekly pay is $pay"
77echo "weekly pay less $( echo "scale=1; $tax_rate * 100"|bc)% taxes is $tax_pay"