Write a bash script named “MyWallet.sh” managing a simple bill system. Every bil
ID: 3684546 • Letter: W
Question
Write a bash script named “MyWallet.sh” managing a simple bill system. Every bill corresponds to a record (line) in a text file named “bills.txt” (if this file is inexistent, create it). Each record consists of 7 fields (record ID, category, detail, amount, year, month, day) separate by percent sign (%).
e.g.:
1%restaurant%subway%5.58%2016%2%11
2%gas station%shell%20.15%2016%3%1
-No percent sign (%) in each field.
-The “record ID” is unique, and generated by this system automatically. (when create a new record, the ID should be the last record’s ID number + 1.)
-The category, detail, and amount are input by user.
-The date (year, month,day) is generated by this system by using the record creation time. (Year: date “+%Y” ; Month: date “+%m” ; Day: date “+%d”)
-MyWallet.sh should provide a menu with following functionalities that users can choose from at the top level menu:
1. Enter “a” to add a new bill • Ask for category
• Ask for detail
• Ask for amount
• Generate record ID and date
• Add this new record into “bills.txt” file
• At the end, print “New bill added successfully”
• Provide option “t” for “Try again” and “b” for “Back to main menu”
2.Enter “d” to remove a bill
• Ask for record ID
• Examine if this record exits. if no, print a message “No such record!”, otherwise, display this record and remove it from “bills.txt” file.
• Provide option “t” for “Try again” and “b” for “Back to main menu”
3.Enter “c” to count amount
• Ask for time range (start date(year, month,day) and end date(year,month,day))
• Count and display amount of all bills in this time period.
• Provide option “t” for “Try again” and “b” for “Back to main menu”
4. Enter “v” to view bills
• Ask for search type (by category, by time, or by record ID)
• Ask for category if by category • Ask for time range if by time
• Ask record ID if by record ID
• Display all bills satisfied the condition
• Provide option “t” for “Try again” and “b” for “Back to main menu”
5.Enter “q” to quit this system
• Terminate this system
Explanation / Answer
#!/bin/bash
#id_values contents intial value 0
RecordID=$(cat /id_values)
RecordID=RecordID+1;
echo "$RecordID" >/id_values
echo "Enter options "
read OPT
enter_details()
{
echo "Please enter category, details and amount"
read CATEGORY
read DETAILS
read AMOUNT
YEAR=$(date+%y);
MONTH=$(date+%m);
DAY=$(date+%d);
}
if [ $OPT="a" ]
then
enter_details
echo "Please confirm to save the data y or n"
read CON
if [ $CON ="y" ]
then
echo "$RecordID,$CATEGORY,$DETAILS,$AMOUNT,$YEAR,$DAY" > > /bill.txt
echo "New bill added successfully"
break;
elif [ $CON="n" ]
enter_details
fi
elif [ $OPT="d" ]
then
echo "Please enter the recordID"
read recordID
sed '$recordID d' /bill.txt
elif [ $OPT="q" ]
then
break;
fi
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.