I need help to complete this assignment please guide me. So here is the assignme
ID: 3570591 • Letter: I
Question
I need help to complete this assignment please guide me.
So here is the assignment (UNIX Shell programming) :
Create an office telephone directory file containing three columns of the last name, first name, and phone number of at least 20 individuals. Then create a shell program that accepts either one (last name) or two (last name and first name) arguments. Check to ensure that one or two arguments are on the command line. If there is only one argument, display a message informing the user that more than one entry might be returned from the phone book. Exit the program if there are less than one or more than two arguments.
Explanation / Answer
function mainmenu {
echo "~~~~~~~~~~~~~~~Main Menu~~~~~~~~~~~~~~"
echo "Enter A to add new number"
echo "Enter D to delete number"
echo "Enter M to modify the existing number"
echo "Enter I to search for a number"
echo "Enter P to disply all the numbers"
echo "Enter X to exit"
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
}
function add {
echo "~~~~~~~~~~ADD USER~~~~~~~~~~"
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "Enter First name.."
read firstName
echo "Enter Last name.."
read lastName
name=$firstName" "$lastName
if [ `grep "$name" /home/mobaxterm/Phonebook_Directory.txt | wc -l` -eq 0 ]
then
echo "Enter 10 digit number.."
read num
if [ `echo "$num" | wc -c` -eq 11 ]
then
echo "$name $num" >> /home/mobaxterm/Phonebook_Directory.txt
rc=$?
if [ $rc -eq 0 ]
then
echo "User added successfully..."
else
echo "Could not add the user. Phonebook directory may be present.."
fi
else
echo "Enter valid 10 digit number.."
fi
else
echo "$name already exists.."
fi
mainmenu
}
function delete {
echo "~~~~~~~~~~DELETE USER~~~~~~~~~~"
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "Enter First name.."
read firstName
echo "Enter Last name.."
read lastName
name=$firstName" "$lastName
if [ `grep "$name" /home/mobaxterm/Phonebook_Directory.txt | wc -l` -eq 0 ]
then
echo "User does not exist..."
else
echo "please confirm the user...(select complete line and press enter)"
numbers=`grep "$name" /home/mobaxterm/Phonebook_Directory.txt | awk -F ':' '{print $1}'`
echo "$numbers"
read conf
linenum=`grep -n "$conf" /home/mobaxterm/Phonebook_Directory.txt | awk -F ':' '{print $1}'`
sed $linenum'd' /home/mobaxterm/Phonebook_Directory.txt > temp.txt
rc=$?
if [ $rc -eq 0 ]
then
cat temp.txt > /home/mobaxterm/Phonebook_Directory.txt
rm temp.txt
echo "User deleted successfully..."
else
echo "Could not delete the user. please select the entire line and press enter.."
fi
fi
mainmenu
}
function modify {
echo "~~~~~~~~~~MODIFY USER~~~~~~~~~~"
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "Enter First name.."
read firstName
echo "Enter Last name.."
read lastName
name=$firstName" "$lastName
if [ `grep "$name" /home/mobaxterm/Phonebook_Directory.txt | wc -l` -eq 0 ]
then
echo "User does not exist..."
else
echo "please confirm the user...(select complete line and press enter)"
numbers=`grep "$name" /home/mobaxterm/Phonebook_Directory.txt | awk -F ':' '{print $1}'`
echo "$numbers"
read conf
linenum=`grep -n "$conf" /home/mobaxterm/Phonebook_Directory.txt | awk -F ':' '{print $1}'`
echo "Enter First name.."
read newfirstName
echo "Enter Last name.."
read newlastName
newname=$newfirstName" "$newlastName
echo "$name"
echo "Enter the number.."
read numb
newNum=$newname" "$numb
echo "$newNum"
echo "$file"
sed $linenum'd' /home/mobaxterm/Phonebook_Directory.txt > temp.txt
rc=$?
if [ $rc -eq 0 ]
then
echo "$newNum" >> temp.txt
cat temp.txt > /home/mobaxterm/Phonebook_Directory.txt
echo "Edited successfully..."
else
echo "Could not edit.."
fi
fi
mainmenu
}
function search {
echo "~~~~~~~~~~SEARCH USER~~~~~~~~~~"
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "Enter First name.."
read firstName
if [ `grep "$firstName" /home/mobaxterm/Phonebook_Directory.txt | wc -l` -eq 0 ]
then
echo "No records present with the name : $firstName"
else
numbs=`grep "$firstName" /home/mobaxterm/Phonebook_Directory.txt | awk -F ':' '{print $1}'`
echo "$numbs"
fi
mainmenu
}
function closephonebook {
exit 0
}
function allnumbers {
cat /home/mobaxterm/Phonebook_Directory.txt
mainmenu
}
function phonebook {
mainmenu
while :
do
read operation
case $operation in
A)
add
;;
D)
delete
;;
M)
modify
;;
I)
search
;;
P)
allnumbers
;;
X)
closephonebook
;;
*)
echo "Invalid choice..."
;;
esac
done
}
echo "Enter User Name.."
read username
Authorization=false
while read userlist
do
if [ "$userlist" == "$username" ]
then
Authorization=true
break
fi
done < /home/mobaxterm/phonebook/password.dat
if [ "$Authorization" = "true" ]
then
echo "Authorised User..."
phonebook
else
echo "Unauthorised User. Cannot open phonebook..."
fi
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.