UNIX SCRIPTING Write 2 shell scripts in bash and perl ( phonebook.bash and phone
ID: 3858933 • Letter: U
Question
UNIX SCRIPTING
Write 2 shell scripts in bash and perl ( phonebook.bash and phonebook.pl ) to maintain a phonebook as specified below.
Program specification
Input: A file of records containing the following fields separated by:
1. FirstName LastName
2. Home Phone Number: (desired format is xxx-xxx-xxxx)
3. Mobile Phone Number: (unique for every person, i.e. the primary key)
4. Address: Street address, City, State and Zip
5. Birth date: MM/DD/YYYY
6. Salary
A sample text file with the desired format:
Output:
The program should provide the user with the following menu options:
- Listing of records in alphabetical order of first name or last name
- Listing of records in reverse alphabetical order of first name or last name
- Search for a record by Last Name and print out the result.
- Search for a record by birthday in a given year or month.
Please also include:
- Insert Record (of course, at the correct location in the sorted list)
- Delete Record from the list by providing the mobile phone number or last name
- Write out the sorted records to the same file before exiting the script.
If not using the sample datafile, then create a text file containing minimum 10 records in the specified format. Your script should reject any record(s) having illegal values, e.g. phone numbers in incorrect format or incorrect zip codes etc., either upon reading the file first or upon a new entry. If you cannot implement format checking for fields due to time constraints, please go ahead and submit what you have.
Feel free to add any other field(s) that you might think is/are important for you to have in a phone book.
You can also add more menu options (functionality) as desired other than the ones mentioned in the next section.
Write your own functions (if allowed by the shell, to do the various menu options)
Programming considerations
Properly document your script by having ample comments throughout the different parts of your scripts
FAQs
Q: Can we have multiple entries in the phone book?
A: No, you cannot, unless one of the fields is different.
Q: What is an illegal zip?
A: Any integer that is less or more than 5 digits long.
Q: What is an illegal phone number?
A: One that does not have 10 digits or is not in the correct format using the hyphens in the correct places. Hence, currently international numbers are not included in the address book.
Q: Do I have to check for illegal address (like incorrect state)?
A: No, but if you can that would be fabulous.
Q: Do I have to check for birth date being a valid date?
A: No, but again, surely try to implement it and I would highly recommend doing it.
Explanation / Answer
phonebook.bash
-----------------------------
#!/bin/bash
if [ $# -eq 0 ] ; then
echo "Please include the Record File as a command line argument and try again"
exit
fi
declare -a array
count=0
file="$1"
while IFS= read -r line
do
name="$line"
array[$count]="$name"
(( count ++ ))
done < "$file"
input=0
createTempFile () {
touch temp
arg=$1
for ((i=0; i < "${#array[@]}"; i++))
do
echo "${array[$i]}" >> temp
if [ $arg == "first" ] ; then
`sort temp -o temp`
elif [ $arg == "last" ] ; then
`sort -k2 temp -o temp`
fi
done
count=0
while IFS= read -r line
do
sorted[$count]="$line"
(( count ++ ))
done < temp
}
sortAlphabetical () {
count=0
while IFS= read -r line
do
sorted[$count]="$line"
(( count ++ ))
done < temp
rm temp
echo "------------------------------------------------------------------------"
if [ $1 == "first" ] ; then
echo "All Records printed in Alphabetical Order by First Name"
else
echo "All Records printed in Alphabetical Order by Last Name"
fi
echo "------------------------------------------------------------------------"
for ((i=0; i < "${#sorted[@]}"; i++))
do
echo ${sorted[$i]}
done
unset sorted
echo "------------------------------------------------------------------------"
}
sortReverse () {
count=0
while IFS= read -r line
do
sorted[$count]="$line"
(( count ++ ))
done < temp
rm temp
echo "------------------------------------------------------------------------"
if [ $1 == "first" ] ; then
echo "All Records printed in Reverse Alphabetical Order by First Name"
else
echo "All Records printed in Reverse Alphabetical Order by Last Name"
fi
echo "------------------------------------------------------------------------"
for ((i=${#sorted[@]}; i >= 0; i--))
do
echo ${sorted[$i]}
done
unset sorted
echo "------------------------------------------------------------------------"
}
searchRecordLastName() {
read -p "Enter last name to be searched: " lastname
echo "------------------------------------------------------------------------"
echo "Record with the Last name" $lastname
echo "------------------------------------------------------------------------"
awk -v var="$lastname" -F'[ :]' '$0 ~ var {print $0}' temp
rm temp
echo "------------------------------------------------------------------------"
}
searchRecordBirthday() {
read -p "Enter date to be searched (Month/Day/Year): " birthday
echo "------------------------------------------------------------------------"
echo "Record with the birtday" $birthday
echo "------------------------------------------------------------------------"
awk -v var="$birthday" -F'[ :]' '$0 ~ var {print $0}' temp
rm temp
echo "------------------------------------------------------------------------"
}
while [ $input != 7 ]
do
declare -a sorted
echo "------------------------------------------------------------------------"
echo "(1) List records in alphabetical order by First Name"
echo "(2) List records in alphabetical order by Last Name"
echo "(3) List records in reverse alphabetical order by First Name"
echo "(4) List records in reverse alphabetical order by Last Name"
echo "(5) Search for a record by Last Name"
echo "(6) Search for a record by Birthday"
echo "(7) Exit"
read -p "Enter an option: " input
case $input in
1)
createTempFile first
sortAlphabetical first
;;
2)
createTempFile last
sortAlphabetical last
;;
3)
createTempFile first
sortReverse first
;;
4)
createTempFile last
sortReverse last
;;
5)
createTempFile first
searchRecordLastName
;;
6)
createTempFile first
searchRecordBirthday
;;
7)
echo "Thank you for using"
exit
;;
*)
echo "Please enter a valid choice"
continue
;;
esac
done
--------------------------------------------------------------------------
phonebook.pl
-----------------------------------------
#!/bin/perl
my $num_args=$#ARGV + 1;
if ($num_args != 1) {
print "Please include the Record File as a command line argument and try again ";
exit;
}
my $filename=$ARGV[0];
open(my $fh, '<:encoding(UTF-8)', $filename)
or die "Could not open file '$filename'";
my $count=0;
while (my $row = <$fh>) {
chomp $row;
$array[$count]=$row;
$count++;
}
sub sortAlphabeticalFirstName {
my @sorted=sort { lc($a) cmp lc($b) } @array;
print "------------------------------------------------------------------------ ";
print "All Records printed in Alphabetical order by First Name ";
print "------------------------------------------------------------------------ ";
for ($i=0; $i<$#array+1; $i++) {
print $sorted[$i], " ";
}
print "------------------------------------------------------------------------ ";
}
sub sortAlphabeticalLastName {
print "------------------------------------------------------------------------ ";
print "All Records printed in Alphabetical order by Last Name ";
print "------------------------------------------------------------------------ ";
my @sorted = sort { (split(/s+/, $b))[1] cmp (split(/s+/, $a))[1] } @array;
for ($i=$#array; $i>=0; $i--) {
print $sorted[$i], " ";
}
print "------------------------------------------------------------------------ ";
}
sub sortReverseFirstName {
print "------------------------------------------------------------------------ ";
print "All Records printed in Reverse Alphabetical order by First Name ";
print "------------------------------------------------------------------------ ";
my @sorted=sort { lc($a) cmp lc($b) } @array;
for ($i=$#sorted; $i>=0; $i--) {
print $sorted[$i], " ";
}
print "------------------------------------------------------------------------ ";
}
sub sortReverseLastName {
print "------------------------------------------------------------------------ ";
print "All Records printed in Reverse Alphabetical order by Last Name ";
print "------------------------------------------------------------------------ ";
my @sorted = sort { (split(/s+/, $b))[1] cmp (split(/s+/, $a))[1] } @array;
for ($i=0; $i<=$#sorted; $i++) {
print $sorted[$i], " ";
}
print "------------------------------------------------------------------------ ";
}
sub searchRecordLastName {
print "Enter last name to be searched: ";
my $lastname = <STDIN>;
chomp $lastname;
print "------------------------------------------------------------------------ ";
print "Record with the Last Name ", $lastname, " ";
print "------------------------------------------------------------------------ ";
for ($i=0; $i<$#array+1; $i++) {
my $strings=$array[$i];
if ($strings =~ /Q$lastnameE/) {
print $array[$i], " ";
}
}
print "------------------------------------------------------------------------ ";
}
sub searchRecordBirthday {
print "Enter birthday to be searched (Month/Day/Year): ";
my $birthday = <STDIN>;
chomp $birthday;
print "------------------------------------------------------------------------ ";
print "Record with the birthday ", $birthday, " ";
print "------------------------------------------------------------------------ ";
for ($i=0; $i<$#array+1; $i++) {
my $strings=$array[$i];
if ($strings =~ /Q$birthdayE/) {
print $array[$i], " ";
}
}
print "------------------------------------------------------------------------ ";
}
my $input=0;
while ($input != 7) {
print "------------------------------------------------------------------------ ";
print "(1) List records in alphabetical order by First Name ";
print "(2) List records in alphabetical order by Last Name ";
print "(3) List records in reverse alphabetical order by First Name ";
print "(4) List records in reverse alphabetical order by Last Name ";
print "(5) Search for a record by Last Name ";
print "(6) Search for a record by Birthday ";
print "(7) Quit ";
print "Enter choice: ";
$input = <STDIN>;
if ($input == 1) {
&sortAlphabeticalFirstName;
}
elsif ($input == 2) {
&sortAlphabeticalLastName;
}
elsif ($input == 3) {
&sortReverseFirstName;
}
elsif ($input == 4) {
&sortReverseLastName;
}
elsif ($input == 5) {
&searchRecordLastName;
}
elsif ($input == 6) {
&searchRecordBirthday;
}
elsif ($input == 7) {
print "Thank you for using ";
exit
}
else {
print "Please enter a valid choice ";
next;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.