A Menu-Driven Application Yonglei Tao. Problem Statement Develop a script program that allows the...

Post on 02-Jan-2016

212 views 0 download

Transcript of A Menu-Driven Application Yonglei Tao. Problem Statement Develop a script program that allows the...

A Menu-Driven Application

Yonglei Tao

Problem Statement Develop a script program that allows the user

to create and maintain an email directory, called mydirectory data is organized as follows

Yonglei Tao:taoy@gvsu.eduJoe Smith:smithj@gmail.com…

Introduction Screen

tput cleartput cup 5 15 echo “An Email Directory Application”echo “ Press any key to continue … “read

Main Screenwhile truedo tput clear

… # display menu options

read choice case $choice in

1) ./add ;;2) ./update;;3) ./search;;4) ./delete;;5) ./print;;6) tput clear ; exit 0 ;;*) ./error ;;

esacdone

Script add

echo –n “Enter name: ”read nameecho –n “Enter email address: ”read address

echo “$name:$address” >> mydirectory

Script search – Version One

echo “Enter name: \c”read target

if grep –iq “$target” mydirectorythen

echo –n “email address is ”gawk –F: ‘$target { print $2}’ mydirectory

elseecho “email address of $target not found”

fi

exit 0

Script search – Version Two

echo “Enter name: \c”read targetgrep –i “$target” mydirectory > temp if [ -s temp ]then

OLD_IFS=”$IFS”IFS=”:”read name address < tempecho “email address is $address”IFS=”$OLD_IFS”

elseecho “email address of $target not found”

fi

exit 0

Script update

echo “Enter name: “read nameecho “Enter new email address: “read address

grep –iv “$name” mydirectory > tempmv temp mydirectoryecho “$name:$address” >> mydirectory

# an alternative waygrep –iv “$name” mydirectory > tempecho “$name:$address” >> tempcat temp | sort > mydirectory

How to do script delete?

Script print-1

sort –f –d –t: mydirectory > temp1

 OLD_IFS=”$IFS”while read name addressdo

echo “Name: $name” >> temp2echo “Email: $address” >> temp2echo >> temp2

done < temp1IFS=”$OLD_IFS” pg –c –p “Page %d: “ temp2rm temp1 temp2

Script print-2

gawk –F: ‘{ printf “Name: %s \n Email: %s \n\n”, $1, $2, }’ mydirectory |less