ShellProject NadaHamarsheh AyaMurra Sec1

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 12

#!

/bin/sh
# Aya Murra 1191426
# Nada Hamarsheh 1190089

touch contact.txt # create a file namd contact.txt


chmod +x contact.txt # change permission of the file contact.txt

# fill the file with some contants


echo "First name, Last name, Number, Gmail" >> contact.txt
echo "Razan, Salahat, 056782456, Razan@gmail.com" >> contact.txt
echo "Nalll, hi, 056722226, Nalll@gmail.com" >> contact.txt
echo "Ahmad, Khalil, 0569345673, Khalil@gmail.com" >> contact.txt
echo "Nada, Hamarsheh, 000000000, " >> contact.txt
echo "Tmara, Khalil, 0569345673, Khalil@gmail.com" >> contact.txt
echo "Aziz, Qaroush, 0599357111;092991111, aqaroush@birzeit.edu" >> contact.txt

# while loop to check if the file is exist


while [ true ]
do
echo "Enter the name of the contant file: "
read file # take the file name from the user

if [ -e "$file" ] # if the file is exist


then # then print that its found and break from the loop
echo "The file has founded"
break
else # if not then print it's not found
echo "the file hasn't found" # and try to enter the file name another time
fi
done

# while loop to print the managment system main menu


while [ true ]
do

printf "\n****welcome to contact managment system***\n"


printf "\t main menu \t\t\n"
printf "\t===========\t\t\n"
printf "[1] add a new contact\n"
printf "[2] list all contact\n"
printf "[3] search for contact\n"
printf "[4] edit a contact\n"
printf "[5] delete a contact\n"
printf "[0] exit\n"
printf "======================"
printf "\n"
echo "Enter youre choice: "
read choice # argument called choice that takes the number of the operation
the user wants

case "$choice" # here is all the operation the usr could do


in
0) exit ;; # if the user enter 0 then the user will exit(quit) from the
program

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

if [ $exit = "yes" ] # to exit from the add contact operation loop


then
break
fi

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

answer="yes" # to let the user insert more then one number


strnumber="" # this variable used to combine the numbers together
count=0 # count the number of tries
while [ "$answer" = "yes" ]
do
echo "==================="
echo "Enter your number: " # let the user enter a number
read number

num=$(echo $number | wc -c) # num variale takes the number of the


charactera that the number contain
num=$(($num-1))

[ $number -eq $number ] 2>/dev/null #to check if the number is


integer (without any alphabets)
if [ \( $? -ne 1 \) -a \( "$num" -ne 9 \) -a \( "$num" -ne 10 \) ] #
check if the number is contain
then # any alphabets and if its
length is more than 10 or less than 9
if [ $count -le 5 ] # if these condition are true
then print an error message and
then # let the user re-enter the
number
echo "==============="
echo "There is Error!"
answer="yes"
count=$(($count+1)) # increament the count by one by every
try it use until it become 6
else
echo "===================="
echo "do you want to exit:" # if number of tries has become 6
then ask the user if he want to continue
read exit # if he say yes then break from
the number loop
if [ $exit = "yes" ]
then
answer="no"
else # if he say no then set the count to 0
one more time and repeat the loop
count=0
fi
fi # after the user enter the number then it's time
to combine it with the other numbers by
else # putting a ; between them
strnumber=""$strnumber""$number"" # that combine the first number
with nothing and save it in the strnumber
# then the reste numbers will
be joined together in one string
echo "======================================================="
echo "Do you want to enter another number: (answer yes or no)" #
ask the user to enter another number
read answer

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

arg="" # if he enter an email make sure that


the email contain @
arg=$(echo "$gmail" | grep "@")

[ -n "$arg" ] # if the email doesn't contain @


if [ "$?" -ne 0 ] # then print an error message and let the user enter
the gmail one more time
then
echo "========="
echo "error"
g="true"
else
g="false"
fi
done
# combine all the contacnt info that the user enter in one line
line=""$firstName","$lastName","$strnumber","$gmail""
echo "$line" >> contact.txt # save the new contant in the contact
file

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

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
rm ftemp
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
rm ftemp
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
rm ftemp S
elif [ $fields -eq 4 ] # if the user want to list all fields then
then # print the file after sorted it
cat temp
fi
fi
rm temp # remove the extra files that used to list 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

while [ $i -lt $n ] # while the variable i is less than number of


word the user enter
do # then search the second word in the fiel that
has the result of the first search
argument=$(echo $argument | cut -d' ' -f2-) # take the rest of the
statement after delete the first word
second=$(echo $argument | cut -d' ' -f1) # take the first word in
the statement after delete the first word

grep -w $second searchtemp > searchtemp1 # search the second


argument in the file
cp -r searchtemp1 searchtemp # overwrite the new search in the
search file
rm searchtemp1
i=$((i+1)) # increment i variable by 1
done
echo "==================================="
cat searchtemp # print the result of the search
echo "==========================================="
echo "do you want to search for another contact? " # ask the user if
he want to search for another contact
read search
done
rm temp searchtemp # remove the extra files that used to search 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

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
the user choose from the main file (contact.txt)

line=$(sed -n ''$i'p' searchtemp | cut -d: -f2-) # take the line


the user want and save it in a line variable
echo $line | tr "," "\n" | tr -d "^ " > temp # replace the ',' in
the line with a new line to separte the information
echo "================================" # into a line and delete
the first space in each line and save it in the temp file
cat temp # print the line ifromation, each info in a line

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

# check which case the user want


if [ $number -eq 1 ]
then
n=0
while [ $n -eq 0 ] # while the edit name isn't contain
alphabet character only repeat the insert
do # until the edit name is contain
alphabets only
echo "$edit" | grep -i -q '^[A-Za-z]*$' # check that the
argument contain alphabets only
if [ $? = 1 ] # if there is any character is't alphabet
then print the error message
then # and let the user re-enter the edit argument
echo "================="
echo "There is Error!"
echo "Enter another name: "
read edit
else
n=1
fi
done
sed -i "${number}s/.*/$edit/" temp # edit the first line
in the temp file

elif [ $number -eq 2 ]


then
if [ $edit = "-" ] # if the user enter '-' then he don't want
the last name
then
lastName=""
else
echo "$lastName" | grep -i -q '^[A-Za-z]*$' # if the user
enter any thing else check
while [ $? = 1 ] # if it's an alphabet
character or not
do
echo "=================="
echo "There is Error!" # if there is any
character is't alphabet then print the error message
echo "Enter another last name: " # and let the user
re-enter the edit argument
read edit
done
fi
sed -i "${number}s/.*/$edit/" temp # edit the second line in
the temp file

elif [ $number -eq 3 ]


then
n=0
while [ $n -eq 0 ]
do
num=$(echo $edit | wc -c) # num variale takes the number
of the charactera that the number contain
num=$(($num-1))
[ $edit -eq $edit ] 2>/dev/null #to check if the number is
integer.
if [ \( $? -ne 1 \) -a \( "$num" -ne 9 \) -a \( "$num" -ne
10 \) ] # check if the number is contain
then # any alphabets and if
its length is more than 10 or less than 9
echo "==================" # if these condition are
true then print an error message and
echo "There is Error!" # let the user re-enter
the edit number
echo "Enter another number: "
read edit
else
n=1
fi
done

numbers=$(sed -n ''$number'p' temp) # take the third line


from the temp file and save it linenumber

echo $numbers | tr ";" "\n" > numbertemp # replace the ';'


with a new line and save if in numbertemp file
anothernumber="yes"
while [ $anothernumber = "yes" ] # loop to let the user edit
another number until he finish and insert no
do
if [ $(cat numbertemp | wc -l) -ge 2 ] # if there is more
than one number then print the lines for the user
then # and the user will
choose any line will edit on it
echo "=================="
cat -n numbertemp # list the number
echo "=================="
echo "choose number line you want to edit: " # choose
which line to edit
read numl

sed -i "${numl}s/.*/$edit/" numbertemp # edit the


chosen line in the numbertemp file

echo "==================" # ask the user if he


want to edit another number
echo "Do you want to edit the another number: "
read anothernumber

if [ $anothernumber = "no" ] # if don't want to edit


then combine the numbers together one more time
then
sed -i '$!s/$/;/' numbertemp
numbers=$(cat numbertemp | tr -d "\n")

sed -i ''$number'd' temp # delete the line that


contain the numbers in the temp file
sed -i '2 a '$numbers'' temp # then insert the new
line after the edit
fi

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

elif [ $number -eq 4 ]


then
n=0
while [ $n -eq 0 ]
do
if [ $edit = "-" ] # if the user enter '-' then he don't
want the gmail
then
edit=""
n=1
else
find=$(echo "$edit" | grep "@") # check if the email
contain @
[ -n "$find" ] # if not then print error message
and let the user enter the gmail one more time
if [ "$?" -ne 0 ]
then
echo "======================"
echo "There is Error!"
echo "Enter another email that contain @: "
read edit
fi
fi
done
sed -i "${number}s/.*/$edit/" temp # edit the fourth line in
the temp file
fi

echo "===================================" # ask the user if he


want to edit another thing in the same contact
echo "Do you want to edit another thing: "
read answer

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

# enter contact info to search it


echo
"================================================================="
echo "enter the search argumant you want to delete: (only one
argument)"
read argument

# set all the alphabets from lowercase


to the uppercase 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
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

rm searchtemp temp # remove the extra files that used to searchon a


contact to delet it
;;

esac
done

You might also like