1
0
Fork 0

Week 14/15: Final Project

This commit is contained in:
Llewellyn van der Merwe 2020-12-17 03:13:06 +02:00
parent c9cfa9f8e4
commit 2c740c9816
Signed by: Llewellyn
GPG Key ID: EFC0C720A240551C
6 changed files with 800 additions and 0 deletions

27
week-15/datafile Normal file
View File

@ -0,0 +1,27 @@
Steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA 83755:11/12/56:20300
Betty Boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
Igor Chevsky:385-375-8395:3567 Populus Place, Caldwell, NJ 23875:6/18/68:23400
Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
Jennifer Cowan:548-834-2348:583 Laurel Ave., Kingsville, TX 83745:10/1/35:58900
James Dean:909-0988-97:__:9/12/89:87589
Jon DeLoach:408-253-3122:123 Park St., San Jose, CA 04086:7/25/53:85100
Karen Evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92743:7/25/53:85100
Fred Fardbarkle:674-843-1385:20 Parak Lane, Duluth, MN 23850:4/12/23:780900
Lori Gortz:327-832-5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
Paco Gutierrez:835-365-1284:454 Easy Street, Decatur, IL 75732:2/28/53:123500
Ephram Hardy:293-259-5395:235 CarltonLane, Joliet, IL 73858:8/12/20:56700
James Ikeda:834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
iuytrertyu iuytrertyu nfdf:567890:rhjyt5678tgg fgh 7t , 7 t tf777:6/8/98:56789
Barbara Kertz:385-573-8326:832 Ponce Drive, Gary, IN 83756:12/1/46:268500
Lesley Kirstin:408-456-1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
William Kopf:846-836-2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
Sir Lancelot:837-835-8257:474 Camelot Boulevard, Bath, WY 28356:5/13/69:24500
Jesse Neal:408-233-8971:45 Rose Terrace, San Francisco, CA 92303:2/3/36:25000
Zippy Pinhead:834-823-8319:2356 Bizarro Ave., Farmount, IL 84357:1/1/67:89500
Arthur Putie:923-835-8745:23 Wimp Lane, Kensington, DL 38758:8/31/69:126000
Popeye Sailor:156-454-3322:945 Bluto Street, Anywhere, USA 29358:3/19/35:22350
Jose Santiago:385-898-8357:38 Fife Way, Abilene, TX 39673:1/5/58:95600
Tommy Savage:408-724-0140:1222 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200
Yukio Takeshida:387-827-1095:13 Uno Lane, Ashville, NC 23556:7/1/29:57000
Vinh Tranh:438-910-7449:8235 Maple Street, Wilmington, VM 29085:9/23/63:68900
Llewellyn van der Merwe:812-487-7770:18 Gos Park, Windhoek, NA 10026:3/12/58:1234567890

28
week-15/datafileE Normal file
View File

@ -0,0 +1,28 @@
Steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA 83755:11/12/56:20300
Betty Boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
Igor Chevsky:385-375-8395:3567 Populus Place, Caldwell, NJ 23875:6/18/68:23400
Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
Jennifer Cowan:548-834-2348:583 Laurel Ave., Kingsville, TX 83745:10/1/35:58900
James Dean:909-0988-97:__:9/12/89:87589
Jon DeLoach:408-253-3122:123 Park St., San Jose, CA 04086:7/25/53:85100
Karen Evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92743:7/25/53:85100
Fred Fardbarkle:674-843-1385:20 Parak Lane, Duluth, MN 23850:4/12/23:780900
Lori Gortz:327-832-5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
Paco Gutierrez:835-365-1284:454 Easy Street, Decatur, IL 75732:2/28/53:123500
Ephram Hardy:293-259-5395:235 CarltonLane, Joliet, IL 73858:8/12/20:56700
James Ikeda:834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
iuytrertyu iuytrertyu nfdf:567890:rhjyt5678tgg fgh 7t , 7 t tf777:6/8/98:56789
Barbara Kertz:385-573-8326:832 Ponce Drive, Gary, IN 83756:12/1/46:268500
Lesley Kirstin:408-456-1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
William Kopf:846-836-2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
Sir Lancelot:837-835-8257:474 Camelot Boulevard, Bath, WY 28356:5/13/69:24500
Mick man:9990-990-990-:wer wrw, werwe, wwerw 990:9/9/99:9980980
Jesse Neal:408-233-8971:45 Rose Terrace, San Francisco, CA 92303:2/3/36:25000
Zippy Pinhead:834-823-8319:2356 Bizarro Ave., Farmount, IL 84357:1/1/67:89500
Arthur Putie:923-835-8745:23 Wimp Lane, Kensington, DL 38758:8/31/69:126000
Popeye Sailor:156-454-3322:945 Bluto Street, Anywhere, USA 29358:3/19/35:22350
Jose Santiago:385-898-8357:38 Fife Way, Abilene, TX 39673:1/5/58:95600
Tommy Savage:408-724-0140:1222 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200
Yukio Takeshida:387-827-1095:13 Uno Lane, Ashville, NC 23556:7/1/29:57000
Vinh Tranh:438-910-7449:8235 Maple Street, Wilmington, VM 29085:9/23/63:68900
Llewellyn van der Merwe:812-487-7770:18 Gos Park, Windhoek, NA 10026:3/12/58:1234567890

225
week-15/final.log Normal file
View File

@ -0,0 +1,225 @@
Script started on 2020-12-19 05:45:36+02:00 [TERM="xterm-256color" TTY="/dev/pts/1" COLUMNS="146" LINES="34"]
]2; ~/Champlain/C/2_Week15_12_Week15_1 » ./loochmod +x lookup*
]2; ~/Champlain/C/2_Week15_12_Week15_1 » ./lookup1
Steve Blenheim 238-923-7366 95 Latham Lane, Easton, PA 83755 11/12/56 20300
Betty Boop 245-836-8357 635 Cutesy Lane, Hollywood, CA 91464 6/23/23 14500
Igor Chevsky 385-375-8395 3567 Populus Place, Caldwell, NJ 23875 6/18/68 23400
Norma Corder 397-857-2735 74 Pine Street, Dearborn, MI 23874 3/28/45 245700
Jennifer Cowan 548-834-2348 583 Laurel Ave., Kingsville, TX 83745 10/1/35 58900
Jon DeLoach 408-253-3122 123 Park St., San Jose, CA 04086 7/25/53 85100
Karen Evich 284-758-2857 23 Edgecliff Place, Lincoln, NB 92743 7/25/53 85100
Fred Fardbarkle 674-843-1385 20 Parak Lane, Duluth, MN 23850 4/12/23 780900
Lori Gortz 327-832-5728 3465 Mirlo Street, Peabody, MA 34756 10/2/65 35200
Paco Gutierrez 835-365-1284 454 Easy Street, Decatur, IL 75732 2/28/53 123500
Ephram Hardy 293-259-5395 235 CarltonLane, Joliet, IL 73858 8/12/20 56700
James Ikeda 834-938-8376 23445 Aster Ave., Allentown, NJ 83745 12/1/38 45000
iuytrertyu iuytrertyu nfdf 567890 rhjyt5678tgg fgh 7t , 7 t tf777 6/8/98 56789
Barbara Kertz 385-573-8326 832 Ponce Drive, Gary, IN 83756 12/1/46 268500
Lesley Kirstin 408-456-1234 4 Harvard Square, Boston, MA 02133 4/22/62 52600
William Kopf 846-836-2837 6937 Ware Road, Milton, PA 93756 9/21/46 43500
Sir Lancelot 837-835-8257 474 Camelot Boulevard, Bath, WY 28356 5/13/69 24500
Mick man 9990-990-990- wer wrw, werwe, wwerw 990 9/9/99 9980980
Jesse Neal 408-233-8971 45 Rose Terrace, San Francisco, CA 92303 2/3/36 25000
Zippy Pinhead 834-823-8319 2356 Bizarro Ave., Farmount, IL 84357 1/1/67 89500
Arthur Putie 923-835-8745 23 Wimp Lane, Kensington, DL 38758 8/31/69 126000
Koos roos __ __ __ __
Popeye Sailor 156-454-3322 945 Bluto Street, Anywhere, USA 29358 3/19/35 22350
Jose Santiago 385-898-8357 38 Fife Way, Abilene, TX 39673 1/5/58 95600
Tommy Savage 408-724-0140 1222 Oxbow Court, Sunnyvale, CA 94087 5/19/66 34200
Yukio Takeshida 387-827-1095 13 Uno Lane, Ashville, NC 23556 7/1/29 57000
Vinh Tranh 438-910-7449 8235 Maple Street, Wilmington, VM 29085 9/23/63 68900
Entries: 27
]2; ~/Champlain/C/2_Week15_12_Week15_1 » ./lookup2
███████████████████████████████████████████████████ ADD ENTRY? ███
Would you like to add an entry to the datafile? [y]
To Quit! [q]
[y/q]: y
██████████████████████████████████████████████████ First Last Name ███
Please enter full name.
NAME FORMAT ==> [first last middle]
[ENTER]: Jso  ason McF Groff    off
█████████████████████████████████████████████████████ Phone Number ███
Please enter phone number.
NUMBER FORMAT ==> [000-000-0000]
[ENTER]: 123-2234-8489
██████████████████████████████████████████████████████████ Address ███
Please enter address.
ADDRESS FORMAT ==> [23 Edgecliff Place, Lincoln, NB 92743]
[ENTER]: 21 Dail This, Where, PA 89212
███████████████████████████████████████████████████████ Birth date ███
Please enter birth date.
DATE FORMAT ==> [7/25/1953]
[ENTER]: 9/11/1982
███████████████████████████████████████████████████████ Salary ███
Please enter salary.
SALARY FORMAT ==> [85100]
[ENTER]: 90000
████████████████████████████████████████████████ ADDED SUCCESS ███
Entry was successfully added!
on-line=>19 Jason McGoff 123-2234-8489 21 Dail This, Where, PA 89212 9/11/1982 90000
███████████████████████████████████████████████████ ADD ENTRY? ███
Would you like to add an entry to the datafile? [y]
To Quit! [q]
[y/q]: q
]2; ~/Champlain/C/2_Week15_12_Week15_1 » ./lookup3
1) Add entry
2) Delete entry
3) View entry
4) View all entries
5) Exit
Please make a selection: 1
██████████████████████████████████████████████████ First Last Name ███
Please enter full name.
NAME FORMAT ==> [first last middle]
[ENTER]: Llewellyn van der Merwe
█████████████████████████████████████████████████████ Phone Number ███
Please enter phone number.
NUMBER FORMAT ==> [000-000-0000]
[ENTER]: 812-487-7770
██████████████████████████████████████████████████████████ Address ███
Please enter address.
ADDRESS FORMAT ==> [23 Edgecliff Place, Lincoln, NB 92743]
[ENTER]: 18 Gos Park, Winh dhk oek, NA 10026
███████████████████████████████████████████████████████ Birth date ███
Please enter birth date.
DATE FORMAT ==> [7/25/1953]
[ENTER]: 3/12/58
███████████████████████████████████████████████████████ Salary ███
Please enter salary.
SALARY FORMAT ==> [85100]
[ENTER]: 1234567890
████████████████████████████████████████████████ ADDED SUCCESS ███
Entry was successfully added!
on-line=>29 Llewellyn van der Merwe 812-487-7770 18 Gos Park, Windhoek, NA 10026 3/12/58 1234567890
████████████████████████████████████████████████████ CONTINUE? ███
Would you like to see the menu again?
[y/N]: y
1) Add entry
2) Delete entry
3) View entry
4) View all
5) Exit
Please make a selection: 3
█████████████████████████████████████████████████ ENTRY SEARCH ███
Please enter any details of entry you would like to view.
[ENTER]: Json
███████████████████████████████████████████████████ NOT FOUND! ███
No entry (Json) found!
████████████████████████████████████████████████████ CONTINUE? ███
Would you like to see the menu again?
[y/N]: y
1) Add entry
2) Delete entry
3) View entry
4) View all
5) Exit
Please make a selection: 3
█████████████████████████████████████████████████ ENTRY SEARCH ███
Please enter any details of entry you would like to view.
[ENTER]: Jason
███████████████████████████████████████████████████████ FOUND! ███
Entry was found!
Jason McGoff 123-2234-8489 21 Dail This, Where, PA 89212 9/11/1982 90000
████████████████████████████████████████████████████ CONTINUE? ███
Would you like to see the menu again?
[y/N]: y
1) Add entry
2) Delete entry
3) View entry
4) View all
5) Exit
Please make a selection: 4
Steve Blenheim 238-923-7366 95 Latham Lane, Easton, PA 83755 11/12/56 20300
Betty Boop 245-836-8357 635 Cutesy Lane, Hollywood, CA 91464 6/23/23 14500
Igor Chevsky 385-375-8395 3567 Populus Place, Caldwell, NJ 23875 6/18/68 23400
Norma Corder 397-857-2735 74 Pine Street, Dearborn, MI 23874 3/28/45 245700
Jennifer Cowan 548-834-2348 583 Laurel Ave., Kingsville, TX 83745 10/1/35 58900
Jon DeLoach 408-253-3122 123 Park St., San Jose, CA 04086 7/25/53 85100
Karen Evich 284-758-2857 23 Edgecliff Place, Lincoln, NB 92743 7/25/53 85100
Fred Fardbarkle 674-843-1385 20 Parak Lane, Duluth, MN 23850 4/12/23 780900
Lori Gortz 327-832-5728 3465 Mirlo Street, Peabody, MA 34756 10/2/65 35200
Paco Gutierrez 835-365-1284 454 Easy Street, Decatur, IL 75732 2/28/53 123500
Ephram Hardy 293-259-5395 235 CarltonLane, Joliet, IL 73858 8/12/20 56700
James Ikeda 834-938-8376 23445 Aster Ave., Allentown, NJ 83745 12/1/38 45000
iuytrertyu iuytrertyu nfdf 567890 rhjyt5678tgg fgh 7t , 7 t tf777 6/8/98 56789
Barbara Kertz 385-573-8326 832 Ponce Drive, Gary, IN 83756 12/1/46 268500
Lesley Kirstin 408-456-1234 4 Harvard Square, Boston, MA 02133 4/22/62 52600
William Kopf 846-836-2837 6937 Ware Road, Milton, PA 93756 9/21/46 43500
Sir Lancelot 837-835-8257 474 Camelot Boulevard, Bath, WY 28356 5/13/69 24500
Mick man 9990-990-990- wer wrw, werwe, wwerw 990 9/9/99 9980980
Jason McGoff 123-2234-8489 21 Dail This, Where, PA 89212 9/11/1982 90000
Jesse Neal 408-233-8971 45 Rose Terrace, San Francisco, CA 92303 2/3/36 25000
Zippy Pinhead 834-823-8319 2356 Bizarro Ave., Farmount, IL 84357 1/1/67 89500
Arthur Putie 923-835-8745 23 Wimp Lane, Kensington, DL 38758 8/31/69 126000
Koos roos __ __ __ __
Popeye Sailor 156-454-3322 945 Bluto Street, Anywhere, USA 29358 3/19/35 22350
Jose Santiago 385-898-8357 38 Fife Way, Abilene, TX 39673 1/5/58 95600
Tommy Savage 408-724-0140 1222 Oxbow Court, Sunnyvale, CA 94087 5/19/66 34200
Yukio Takeshida 387-827-1095 13 Uno Lane, Ashville, NC 23556 7/1/29 57000
Vinh Tranh 438-910-7449 8235 Maple Street, Wilmington, VM 29085 9/23/63 68900
Llewellyn van der Merwe 812-487-7770 18 Gos Park, Windhoek, NA 10026 3/12/58 1234567890
Entries: 29
████████████████████████████████████████████████████ CONTINUE? ███
Would you like to see the menu again?
[y/N]: y
1) Add entry
2) Delete entry
3) View entry
4) View all
5) Exit
Please make a selection: 2
██████████████████████████████████████████████ ENTRY TO DELETE ███
Please enter full name of the entry you would like to delete.
NAME FORMAT ==> [first last middle]
[ENTER]: Llewe
███████████████████████████████████████████████████ NOT FOUND! ███
No entry (Llewe) found!
████████████████████████████████████████████████████ CONTINUE? ███
Would you like to see the menu again?
[y/N]: y
1) Add entry
2) Delete entry
3) View entry
4) View all
5) Exit
Please make a selection: 2
██████████████████████████████████████████████ ENTRY TO DELETE ███
Please enter full name of the entry you would like to delete.
NAME FORMAT ==> [first last middle]
[ENTER]: Jason McGoff
███████████████████████████████████████████████ ENTRY DELETED! ███
Entry was found and deleted!
████████████████████████████████████████████████████ CONTINUE? ███
Would you like to see the menu again?
[y/N]: y
1) Add entry
2) Delete entry
3) View entry
4) View all
5) Exit
Please make a selection: 5
Goodbye! Llewellyn
]2; ~/Champlain/C/2_Week15_12_Week15_1 » exit
exit
Script done on 2020-12-19 05:51:18+02:00 [COMMAND_EXIT_CODE="0"]

35
week-15/lookup1 Executable file
View File

@ -0,0 +1,35 @@
#!/bin/bash
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
# Champlain College SDEV-415-81
#
# Linux/Unix Programming Week 14/15: Final Project - (2020/12/18)
#
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#
# This script is to display datafile in sorted order using last names.
# The datafile has the following format:
# First Last Name:Phone Number:Address:Birth date:Salary
# Karen Evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92743:7/25/53:85100
#
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#
# Written by Llewellyn van der Merwe <llewellyn.vandermerw@mymail.champlain.edu>, December 2020
# Copyright (C) 2020. All Rights Reserved
# License GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html
#
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
# Display the sorted data as a table from file
sort -k2 datafile | column -t -s :
# above code explained:
# sort get the file content
# sort use space to detect columns
# sort use -k2 to identify last names
# sort by this 2nd column (last names)
# pipe pass data to next command
# column convert the data to formatted table
# column use the -t -s : to break up the columns
# column prints data to standard output.
# display the number of lines/entries in the file
echo -n "Entries: "; cat datafile | wc -l

164
week-15/lookup2 Executable file
View File

@ -0,0 +1,164 @@
#!/bin/bash
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
# Champlain College SDEV-415-81
#
# Linux/Unix Programming Week 14/15: Final Project - (2020/12/18)
#
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#
# This script is to add an entry to the datafile and sort the file in place
# The datafile has the following format:
# First Last Name:Phone Number:Address:Birth date:Salary
# Karen Evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92743:7/25/53:85100
#
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#
# Written by Llewellyn van der Merwe <llewellyn.vandermerw@mymail.champlain.edu>, December 2020
# Copyright (C) 2020. All Rights Reserved
# License GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html
#
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#█████████████████████████████████████████████████████████ Function - Main ███
function main() {
# the basic program
while true;
do
# ask the user if they would like to add a entry to the datafile
printo "███████████████████████████████████████████████████ ADD ENTRY? ███" H1
printo "Would you like to add an entry to the datafile? [y]" Q
# give the quit notice
printo "To Quit! [q]" F
echo -n " [y/q]: "
read -r answer
if [ ${answer^^} == "Y" ] || [ ${answer^^} == "YES" ]; then
# get the new entry
if getNewEntry; then
# sort the file
sortDataFile
# give success notice
printo "████████████████████████████████████████████████ ADDED SUCCESS ███" S
printo "Entry was successfully added!" S
# show line and line number where the entry was added
echo -n "on-line=>"; grep -En "^${FULL_NAME}:" datafile | column -t -s :
else
# show error on failure
printo "██████████████████████████████████████████████████████ FAILURE ███" F
printo "Entry failed!" F
fi
elif [ ${answer^^} == "Q" ]; then
exit 0
fi
done
# we should not get here
exit 1
}
#█████████████████████████████████████████████████████████████ Color Codes ███
normorange="\033[0;33m"
lightGreen="\033[1;32m"
lightCyan="\033[1;36m"
lightRed="\033[1;31m"
NC="\033[0m"
#████████████████████████████████████████████████████████ Function - PRINT ███
function printo() {
if [ -z "$2" ]; then
echo -e "$1"
elif [ "$2" == "S" ]; then
echo -e "${lightGreen}$1${NC}"
elif [ "$2" == "F" ]; then
echo -e "${lightRed}$1${NC}"
elif [ "$2" == "H1" ]; then
echo -e "${lightCyan}$1${NC}"
elif [ "$2" == "Q" ]; then
echo -e "${normorange}$1${NC}"
fi
}
#████████████████████████████████████████████ Function - Inplace sort data ███
function sortDataFile() {
# Sorted data in place
sort -k2 -o datafile datafile
# above code explained:
# sort get the file content
# sort use space to detect columns
# sort use -k2 to identify last names
# sort by this 2nd column (last names)
}
#███████████████████████████████████████████████ Function - Get new entry ███
function getNewEntry() {
echo
# Ask the user the full name
printo "██████████████████████████████████████████████████ First Last Name ███" H1
printo "Please enter full name." Q
printo "NAME FORMAT ==> [first last middle]" Q
echo -n " [ENTER]: "
read -r full_name
# force name to be given
while [[ ${#full_name} = 0 ]]; do
printo "Empty Name not allowed" F
printo "NAME FORMAT ==> [first last middle]" Q
echo -n " [ENTER]: "
read -r full_name
done
echo
# Ask the user the Phone Number
printo "█████████████████████████████████████████████████████ Phone Number ███" H1
printo "Please enter phone number." Q
printo "NUMBER FORMAT ==> [000-000-0000]" Q
echo -n " [ENTER]: "
read -r phone_number
# pad empty phone_number
if [[ ${#phone_number} = 0 ]]; then
phone_number="__"
fi
echo
# Ask the user the Address
printo "██████████████████████████████████████████████████████████ Address ███" H1
printo "Please enter address." Q
printo "ADDRESS FORMAT ==> [23 Edgecliff Place, Lincoln, NB 92743]" Q
echo -n " [ENTER]: "
read -r address
# pad empty phone_number
if [[ ${#address} = 0 ]]; then
address="__"
fi
echo
# Ask the user the Birth date
printo "███████████████████████████████████████████████████████ Birth date ███" H1
printo "Please enter birth date." Q
printo "DATE FORMAT ==> [7/25/1953]" Q
echo -n " [ENTER]: "
read -r birth_date
# pad empty phone_number
if [[ ${#birth_date} = 0 ]]; then
birth_date="__"
fi
echo
# Ask the user the Salary
printo "███████████████████████████████████████████████████████ Salary ███" H1
printo "Please enter salary." Q
printo "SALARY FORMAT ==> [85100]" Q
echo -n " [ENTER]: "
read -r salary
# pad empty phone_number
if [[ ${#salary} = 0 ]]; then
salary="__"
fi
# now add entry to file
echo "${full_name}:${phone_number}:${address}:${birth_date}:${salary}" >> datafile
# move name global for search
export FULL_NAME=${full_name}
# return successfully added
return 0
}
#█████████████████████████████████████████████████████████████ RUN MAIN ;) ███
main
#█████████████████████████ Display the number of lines/entries in the file ███
echo
echo -n "Entries: "
cat datafile | wc -l

321
week-15/lookup3 Executable file
View File

@ -0,0 +1,321 @@
#!/bin/bash
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
# Champlain College SDEV-415-81
#
# Linux/Unix Programming Week 14/15: Final Project - (2020/12/18)
#
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#
# This script is add, delete, and view entries in the datafile
# The datafile has the following format:
# First Last Name:Phone Number:Address:Birth date:Salary
# Karen Evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92743:7/25/53:85100
#
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#
# Written by Llewellyn van der Merwe <llewellyn.vandermerw@mymail.champlain.edu>, December 2020
# Copyright (C) 2020. All Rights Reserved
# License GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html
#
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#█████████████████████████████████████████████████████████ Function - Main ███
function main() {
# we first check if our data file exist and is readable and writable
if [ ! -e datafile ]; then
# give console error
printo "The datafile does not exist." F
# break out here
exit 1
elif [ ! -w datafile ]; then
# give console error
printo "The datafile is not writable." F
# break out here
exit 1
fi
# some house cleaning
export PS3_old=$PS3
# some defaults
export PS3="Please make a selection: "
# Start our little Menu
select menu in \
'Add entry' \
'Delete entry' \
'View entry' \
'View all entries' \
'Exit'
do
case $REPLY in
1 ) addEntry;;
2 ) deleteEntry;;
3 ) viewEntry;;
4 ) viewEntries;;
5 )
# final greeting
echo
printo "Goodbye! ${USER^}" S
echo
# restore the default
export PS3=$PS3_old
exit 0;;
* )
printo "Invalid entry, try again." F
printo "Select option 1..5" F
displayMenu;;
esac
# we can add a normal break here if the loop must just run once
# break;
done
# restore the default
export PS3=$PS3_old
# we should not get here
exit 1
}
#███████████████████████████ Function - To check if user wants to continue ███
function continueOrNot(){
# ask the user if they would like to see the menu again
printo "████████████████████████████████████████████████████ CONTINUE? ███" H1
printo "Would you like to see the menu again?" Q
echo -n " [y/N]: "
read -r answer
if [ ${answer^^} == "Y" ] || [ ${answer^^} == "YES" ]; then
displayMenu
return 0
else
# final greeting
echo
printo "Goodbye! ${USER^}" S
echo
# restore the default
export PS3=$PS3_old
exit 0;
fi
}
#█████████████████████████████████████████████████ Function - Display Menu ███
function displayMenu(){
echo
echo "1) Add entry"
echo "2) Delete entry"
echo "3) View entry"
echo "4) View all"
echo "5) Exit"
}
#████████████████████████████████████████████████████ Function - Add Entry ███
function addEntry() {
# get the new entry
if getNewEntry; then
# sort the file
sortDataFile
# give success notice
printo "████████████████████████████████████████████████ ADDED SUCCESS ███" S
printo "Entry was successfully added!" S
# show line and line number where the entry was added
echo -n "on-line=>"; grep -En "^${FULL_NAME}:" datafile | column -t -s :
else
# get the exit status
why_fail=$?
# show error on failure
printo "██████████████████████████████████████████████████████ FAILURE ███" F
printo "Entry failed!" F
# check exist status
if [ $why_fail == 2 ]; then
printo "Since (${FULL_NAME}) already exist in file!" F
fi
fi
# check if we are continuing
continueOrNot
}
#█████████████████████████████████████████████████ Function - Delete Entry ███
function deleteEntry() {
# Ask the user the full name of the entry they would like to view
printo "██████████████████████████████████████████████ ENTRY TO DELETE ███" H1
printo "Please enter full name of the entry you would like to delete." Q
printo "NAME FORMAT ==> [first last middle]" Q
echo -n " [ENTER]: "
read -r search_value
# force value to be given
while [[ ${#search_value} = 0 ]]; do
printo "Empty value not allowed" F
echo -n " [ENTER]: "
read -r search_value
done
# if found remove
if grep -Eq "^${search_value}:" datafile; then
# now remove from file
sed -iE "/^${search_value}:/d" datafile
# give success found notice
printo "███████████████████████████████████████████████ ENTRY DELETED! ███" S
printo "Entry was found and deleted!" S
else
# give not found notice
printo "███████████████████████████████████████████████████ NOT FOUND! ███" F
printo "No entry (${search_value}) found!" F
fi
# check if we are continuing
continueOrNot
}
#███████████████████████████████████████████████████ Function - View Entry ███
function viewEntry() {
# Ask the user the full name of the entry they would like to view
printo "█████████████████████████████████████████████████ ENTRY SEARCH ███" H1
printo "Please enter any details of entry you would like to view." Q
echo -n " [ENTER]: "
read -r search_value
# force value to be given
while [[ ${#search_value} = 0 ]]; do
printo "Empty value not allowed" F
echo -n " [ENTER]: "
read -r search_value
done
# check if this value exist
if fgrep -iq "${search_value}" datafile; then
# give success found notice
printo "███████████████████████████████████████████████████████ FOUND! ███" S
printo "Entry was found!" S
# show line and line number where the entry was added
grep -iE "${search_value}" datafile | column -t -s :
else
# give not found notice
printo "███████████████████████████████████████████████████ NOT FOUND! ███" F
printo "No entry (${search_value}) found!" F
fi
# check if we are continuing
continueOrNot
}
#█████████████████████████████████████████████████ Function - View Entries ███
function viewEntries() {
# Display the sorted data as a table from file
sort -k2 datafile | column -t -s :
# above code explained:
# sort get the file content
# sort use space to detect columns
# sort use -k2 to identify last names
# sort by this 2nd column (last names)
# pipe pass data to next command
# column convert the data to formatted table
# column use the -t -s : to break up the columns
# column prints data to standard output.
# display the number of lines/entries in the file
echo
echo -n "Entries: "
cat datafile | wc -l
echo
# check if we are continuing
continueOrNot
}
#█████████████████████████████████████████████████████████████ Color Codes ███
normorange="\033[0;33m"
lightGreen="\033[1;32m"
lightCyan="\033[1;36m"
lightRed="\033[1;31m"
NC="\033[0m"
#████████████████████████████████████████████████████████ Function - PRINT ███
function printo() {
if [ -z "$2" ]; then
echo -e "$1"
elif [ "$2" == "S" ]; then
echo -e "${lightGreen}$1${NC}"
elif [ "$2" == "F" ]; then
echo -e "${lightRed}$1${NC}"
elif [ "$2" == "H1" ]; then
echo -e "${lightCyan}$1${NC}"
elif [ "$2" == "Q" ]; then
echo -e "${normorange}$1${NC}"
fi
}
#████████████████████████████████████████████ Function - Inplace sort data ███
function sortDataFile() {
# Sorted data in place
sort -k2 -o datafile datafile
# above code explained:
# sort get the file content
# sort use space to detect columns
# sort use -k2 to identify last names
# sort by this 2nd column (last names)
}
#███████████████████████████████████████████████ Function - Get new entry ███
function getNewEntry() {
echo
# Ask the user the full name
printo "██████████████████████████████████████████████████ First Last Name ███" H1
printo "Please enter full name." Q
printo "NAME FORMAT ==> [first last middle]" Q
echo -n " [ENTER]: "
read -r full_name
# force name to be given
while [[ ${#full_name} = 0 ]]; do
printo "Empty Name not allowed" F
printo "NAME FORMAT ==> [first last middle]" Q
echo -n " [ENTER]: "
read -r full_name
done
echo
# move name global for later
export FULL_NAME=${full_name}
# check if entry exist in file
if grep -Eq "^${full_name}:" datafile; then
# return failure added
return 2
fi
# Ask the user the Phone Number
printo "█████████████████████████████████████████████████████ Phone Number ███" H1
printo "Please enter phone number." Q
printo "NUMBER FORMAT ==> [000-000-0000]" Q
echo -n " [ENTER]: "
read -r phone_number
# pad empty phone_number
if [[ ${#phone_number} = 0 ]]; then
phone_number="__"
fi
echo
# Ask the user the Address
printo "██████████████████████████████████████████████████████████ Address ███" H1
printo "Please enter address." Q
printo "ADDRESS FORMAT ==> [23 Edgecliff Place, Lincoln, NB 92743]" Q
echo -n " [ENTER]: "
read -r address
# pad empty phone_number
if [[ ${#address} = 0 ]]; then
address="__"
fi
echo
# Ask the user the Birth date
printo "███████████████████████████████████████████████████████ Birth date ███" H1
printo "Please enter birth date." Q
printo "DATE FORMAT ==> [7/25/1953]" Q
echo -n " [ENTER]: "
read -r birth_date
# pad empty phone_number
if [[ ${#birth_date} = 0 ]]; then
birth_date="__"
fi
echo
# Ask the user the Salary
printo "███████████████████████████████████████████████████████████ Salary ███" H1
printo "Please enter salary." Q
printo "SALARY FORMAT ==> [85100]" Q
echo -n " [ENTER]: "
read -r salary
# pad empty phone_number
if [[ ${#salary} = 0 ]]; then
salary="__"
fi
# now add entry to file
echo "${full_name}:${phone_number}:${address}:${birth_date}:${salary}" >> datafile
# return successfully added
return 0
}
#█████████████████████████████████████████████████████████████ RUN MAIN ;) ███
main