Professional Documents
Culture Documents
ShellProject NadaHamarsheh AyaMurra Sec1
ShellProject NadaHamarsheh AyaMurra Sec1
ShellProject NadaHamarsheh AyaMurra Sec1
/bin/sh
# Aya Murra 1191426
# Nada Hamarsheh 1190089
1) new="yes" # if the user enter 1 then its time to enter a new contact
exit="no"
while [ $new = "yes" ] # while the new variable equal to 'yes' then
do # the user can enter another new contact
atempt=0 # this variable used to give the user choices to enter the
first
# and then he can exit the program
echo "=============================================="
echo "you will enter your information as it ask you "
echo "if you don't want to answer insert '-' "
echo "=============================================="
echo "Enter your first name: " # let the user enter the first name
read firstName # and it's an important info, which
must be there
echo "$firstName" | grep -i -q '^[A-Za-z]*$' # this command check if
the name is from an alphabets only
while [ $? = 1 ] # if all the name is not an alphabets then print that
its an error
do # and let the user enter the name one more time
for 6 tries
if [ $atempt -le 5 ] # more the 6 the program will ask the user if he
wants to continue trying or exit this operation
then
echo "====================================="
echo "Error, You Must enter your first name"
echo "Enter your first name again: "
read firstName # enter the name one more time
atempt=$(($atempt+1)) # increament the number of tries by one
else
echo "==================="
echo "do you want to exit:" # after the 6 tries has been done ask
the user if he want to exit
read exit
if [ $exit = "yes" ] # if yes break from the first name loop
then break from the operation loop
then
break
else
atempt=0 # if not (he want to continue) then return the try
variable to 0
fi
fi
done
echo "===================="
echo "Enter your last name: " # now ask the user to enter the last name
read lastName
if [ $lastName = "-" ] # if the useer enter '-' then the last name
will be empty
then
lastName=""
else # if the user enter the name, then check if
the name contain any numbers,..
echo "$lastName" | grep -i -q '^[A-Za-z]*$' # here we check if the
last name contain any numbers
while [ $? = 1 ] # if it contain any thing exept the alphabets
do # then let the user enter the last
name one more time
echo "==========================="
echo "Enter your last name again: "
read lastName
done
fi
if [ "$answer" = "yes" ]
then
strnumber=""$strnumber";" # every number will be enterd will
have a ; after it until the user done from insert
fi
fi
done
if [ $exit = "yes" ] # this to exit from the add operation loop if the
user dont want to continue insert
then
break
fi
g="true"
while [ "$g" = "true" ] # a while loop to enter the gmail
do
echo "================="
echo "Enter your emai: " # ask user to enter the email
read gmail
if [ "$gmail" = "-" ] # if the user enter '-' then put nothing in the
contant email
then
gmail=""
break;
fi
echo "do you want to enter another contact:" # ask the user if he want
to enter other contacts
read new
done
;;
2) echo "======================="
echo "Choose the based list: " # if the user enter 2 then it's time to
list all the contact as a sorted file
echo "1- First name" # based on the first name or the last
name
echo "2- Last name"
read base # ask the user to enter his based choice
echo "================================"
echo "which fields you want to appear:" # ask the user to enter which
fields he want to have in the list
echo "1- Frist name, Last name"
echo "2- Frist name, Last name, Number"
echo "3- Frist name, Last name, Gmail"
echo "4- ALL"
read fields
if [ "$base" -eq 1 ] # if the user choice the base 1 then sort the
file according to the first name
then
head -1 contact.txt > temp; # take the first line without sorted
it with the file
tail -n+2 contact.txt | sort -n >> temp # sort the rest of the file
if [ $fields -eq 1 ] # if the user want to list only the first two
fields then
then # cut the columns in the file to apear the
first two column only
cat temp | cut -d ',' -f1,2 > ftemp
cat ftemp # print the file after sorted it and delete
the unwanted fields
elif [ $fields -eq 2 ] # if the user want to list only the first
three fields then
then # cut the columns in the file to apear the
first three column only
cat temp | cut -d ',' -f1,2,3 > ftemp
cat ftemp # print the file after sorted it and delete
the unwanted fields
elif [ $fields -eq 3 ] # if the user want to list only the first two
and the last fields then
then # cut the columns in the file to apear the
first two and last column only
cat temp | cut -d ',' -f1,2,4 > ftemp
cat ftemp # print the file after sorted it and delete the
unwanted fields
elif [ $fields -eq 4 ] # if the user want to list all fields then
then # print the file after sorted it
cat temp
fi
elif [ "$base" -eq 2 ] # if the user choice the base 2 then sort the
file according to the last name
then
head -1 contact.txt > temp; # take the first line without sorted it
with hte file
tail -n+2 contact.txt | sort -t',' -k2,2 >> temp # sort the rest of
the file
3) search="yes"
while [ $search = "yes" ] # while the search variable equal to 'yes'
then
do # the user can search for another contact
i=1
sed 's/[a-z]/\U&/g' contact.txt > temp # set all the alphabets from
lowercase to the uppercase
sed -i '1d' temp # delete the first line from the file , which
contain the column discreption
echo "=========================="
echo "enter the search argumant:" # enter contact info to search it
read argument
argument=$(echo $argument | sed 's/[a-z]/\U&/g') # set all the
alphabets from lowercase to the uppercase
n=$(echo "$argument" | wc -w) # count how many words did the user
enter
first=$(echo $argument | cut -d' ' -f1) # take the first word the user
enter
grep -w $first temp > searchtemp # and search it in the file
4) editnew="yes"
while [ $editnew = "yes" ] # while the editnew variable equal to 'yes'
then
do # the user can search for another contact
sed 's/[A-Z]/\L&/g' contact.txt > temp # set all the alphabets from
uppercase to the lowercase
sed -i '1d' temp # delete the first line from the file ,
which contain the column discreption
echo "==============================================================="
echo "enter the search argumant you want to edit: (only one argument)"
# enter contact info to search it
read argument
# set all the alphabets from uppercase to the
lowercase in the first argument
argument=$(echo $argument | cut -d' ' -f1 | sed 's/[A-Z]/\L&/g')
grep -n -w $argument temp > searchtemp # search it in the file
if [ -s searchtemp ]
then
echo "========================================"
cat -n searchtemp # print the search result and number the lines
i=0
echo "========================================"
echo "Enter the line number you want to edit: " # let the user
enter the line number he want to edit
read i
answer="yes"
while [ $answer = "yes" ] # while the search variable equal to
'yes' then
do # the user can edit another information
for the same contact
echo "===================================="
echo "Choose which feld you wnat to edit: "
echo "1- First name"
echo "2- Last name"
echo "3- Number"
echo "4- Gmail"
read number # ask the user to choose which field he want
to edit
echo "===================="
echo "Enter the new edit: "
read edit # ask the user to enter new word
else
sed -i "${number}s/.*/$edit/" temp # edit the third
line in the temp file
break
fi
rm numbertemp # remove the extra files that used to edit
the numbers
done
done
sed -i '$!s/$/, /' temp # insert a ',' at the end of each line
exept the last line in temp file
echo $(cat temp | tr -d "\n") >> contact.txt # delete the new
lines to make it one line then add it to the main file
echo "====================================="
echo "do you want to edit another contact: " # ask the user if he
want to edit something on another contact
read editnew
fi
done
echo "========================================"
cat -n contact.txt # print the contact file after the edit
rm temp searchtemp # remove the extra files that used to edit the
contact information
;;
5) delete="yes"
while [ $delete = "yes" ] # loop lets the user delete more then one
contact
do
sed 's/[A-Z]/\L&/g' contact.txt > temp # set all the alphabets from
lowercase to the uppercase
sed -i '1d' temp # delete the first line from the file , which
contain the column discreption
if [ -s searchtemp ]
then
echo "========================================"
cat -n searchtemp # print the search result
i=0
echo "==========================================="
echo "Enter the line number you want to delete: " # let the user
enter the line he want to delete
read i
linenumber=$(sed -n ''$i'p' searchtemp | cut -d: -f1) # take the
line number as if in the contactnumber
linenumber=$(($linenumber+1))
sed -i ''$linenumber'd' contact.txt # delete the line from the
contact file
echo "====================================="
echo "do you want to delete another contact: " # ask the user if
he want to delete another contact
read delete
fi
done
echo "========================================"
cat -n contact.txt # print the contact file after the delete
esac
done