Using bash (not a higher level language), construct a program which manages cust
ID: 3883322 • Letter: U
Question
Using bash (not a higher level language), construct a program which manages customer files. Each customer has a separate file. Your program will interactively manage customer accounts Customer File Content One file per customer, named the customer's email address Each customer file contains two records: Identification record which has two attributes: Customer Email Customer Full Name Account record which has four attributes: Apartment Number format (APT-xx) Example customer file contents: board@xyz.com Bill Board APT-5A 1100.00 0.00 2017-10-01 8 Action Menu Display a menu which shows Enter one of the following actions or press CTRL-D to exit. C-create a customer file P- accept a customer payment F- find customer by apartm ent number Read a one character action. Based on the action, do the following: C- CREATE - create a customer file From the terminal, read (each from separate read requests) Customer Full Name Monthly Rent Amt Next Due Date .Default the Account Balance to zero Create a new customer file using the customer's email as the file name. If the file already exists, show an error message (and do nothing) Assume that each of the five attributes should be read before checking for prior existence. P- PAYMENT - accept customer payment From the terminal, read Customer Email Payment Amount Update the customer's file, adding the payment amount to the Account Balance * If the customer doesn't exist, show an error message (and do nothing) Assume that each of the two attributes should be read before checking for existenceExplanation / Answer
ActionMenu.sh
--------------------------------
#!/bin/bash
go=0
while [ $go ]; do
echo "Enter one of the following actions or press CTRL-D to exit."
echo "C - create a customer file"
echo "P - accept a customer payment"
echo "F - find customer by apartment number"
if ! read action; then
break;
fi
# echo "Action = $action"
case "$action" in
[Cc]) ./CreateAction.sh;; # Run CreateAction.sh
[Pp]) ./PaymentAction.sh;; # Run PaymentAction.sh
[Ff]) ./FindAction.sh;; # Run FindAction.sh
*) echo -e "Error: invalid action value ";; # Redisplay action menu
esac
done
-------------------------------------------------------
CreateAction.sh
-----------------------------
#!/bin/bash
read -p "Email: " email
read -p "Full Name (First Last): " firstname lastname
read -p "Apt: APT-" apt
read -p "Monthly Rent Amount: $" monthlyrent
read -p "Next Due Date: " duedate
balance=0
# See if customer exists, look in Data directory
if [ $(find ./Data -name "$email" | wc -l) -eq 1 ]; then # gt 0 ? eq 1
echo -e "Error: customer already exists "
# Do nothing
else
# Create Customer File
# echo "Creating Customer File..."
echo "$email $firstname $lastname" > $email # Redirect to file
echo "$apt $monthlyrent $balance $duedate" >> $email # Append to file
# echo completion
echo -e " ------ New Customer ------"
echo "$email $firstname $lastname"
echo -e "$apt $monthlyrent $balance $duedate "
fi
----------------------------------------------------------
FindAction.sh
-----------------------------------
#!/bin/bash
# set -x # echo on
read -p "Apartment number (format: APT-xx): APT-" apt
# Check if apartment exists in any of the customer files
if [ $(grep -rw "./Data" -e "$apt" | wc -l) -eq 1 ]; then
# if [[ $(wc -l < "${filename[*]}") -eq 1 ]]; then # Having trouble with
filename=$(grep -rwl "./Data" -e "$apt") #r - recursive, w - word match, l - filenames that contain match
items=()
while read -r line || [[ -n "$line" ]]; do # Deals with file that does not have at end to get last line
items+=( $line )
# echo "line = $line"
done < $filename
# declare | grep $filename
# declare | grep $line_array
# declare | grep 'items'
# echo "line_array = ${line_array[@]}"
# echo "lines_array = ${lines_array[@]}"
# echo "items = ${items[@]}"
# Print relevant information
echo -e " -----Customer------"
echo "Email: ${items[0]}"
echo "Name: ${items[1]} ${items[2]}"
echo "Apt: ${items[3]}"
echo "Monthly Rent Amount: $${items[4]}"
echo -e "Next Due Date: ${items[6]} "
else
echo -e "Error: apartment number not found "
# Do nothing
fi
--------------------------------------------------------
PaymentAction.sh
--------------------------
#!/bin/bash
read -p "Email: " email
read -p "Payment Amount: $" amount
if [ $(find ./Data -name "$email" | wc -l) -eq 1 ]; then # gt 0 ? eq 1
items=()
items+=($(sed '2q;d' ./Data/$email)) # deserializes 2nd line into an array
# declare | grep 'items'
((items[2] = ${items[2]} + $amount)) # Convert to integer and add to balance
# declare | grep 'items'
# replace the 2nd line with the new 2nd line and updated account balance
sed -i "2s/.*/${items[*]}/" ./Data/$email
# sed "2s/.*/${items[*]}/" $email > dummy.txt # need asterick
# slower way to do it using array slicing
# items=()
# while read line; do
# items+=( $line )
# # echo "line = $line"
# done < $email
# declare | grep 'items'
# # Add amount to payment
# ((newBalance = ${items[5]} + $amount))
# echo "$newBalance"
# declare | grep '$items'
# echo ${items[@]:0:3} #> dummy.txt
# echo "${items[@]:3:2} $newBalance ${items[6]}" #>> dummy.txt
else
echo -e "Error: customer not found "
# Do nothing
fi
----------------------------------------------------------
p1Input.txt
-----------------------------
C
polyester@material.com
Poly Ester
APT-2C
900
2017-11-01
P
aking@xyz.com
900
C
board@xyz.com
Peg Board
APT-2F
900
2017-11-02
F
APT-3D
F
APT-3F
P
bhead@xyz.com
950
P
perrymecium@bio.com
1000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.