a menu-driven application yonglei tao. problem statement develop a script program that allows the...

10
A Menu-Driven Application Yonglei Tao

Upload: randall-cooper

Post on 02-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A Menu-Driven Application Yonglei Tao. Problem Statement  Develop a script program that allows the user to create and maintain an email directory, called

A Menu-Driven Application

Yonglei Tao

Page 2: A Menu-Driven Application Yonglei Tao. Problem Statement  Develop a script program that allows the user to create and maintain an email directory, called

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:[email protected] Smith:[email protected]

Page 3: A Menu-Driven Application Yonglei Tao. Problem Statement  Develop a script program that allows the user to create and maintain an email directory, called

Introduction Screen

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

Page 4: A Menu-Driven Application Yonglei Tao. Problem Statement  Develop a script program that allows the user to create and maintain an email directory, called

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

Page 5: A Menu-Driven Application Yonglei Tao. Problem Statement  Develop a script program that allows the user to create and maintain an email directory, called

Script add

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

echo “$name:$address” >> mydirectory

Page 6: A Menu-Driven Application Yonglei Tao. Problem Statement  Develop a script program that allows the user to create and maintain an email directory, called

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

Page 7: A Menu-Driven Application Yonglei Tao. Problem Statement  Develop a script program that allows the user to create and maintain an email directory, called

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

Page 8: A Menu-Driven Application Yonglei Tao. Problem Statement  Develop a script program that allows the user to create and maintain an email directory, called

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?

Page 9: A Menu-Driven Application Yonglei Tao. Problem Statement  Develop a script program that allows the user to create and maintain an email directory, called

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

Page 10: A Menu-Driven Application Yonglei Tao. Problem Statement  Develop a script program that allows the user to create and maintain an email directory, called

Script print-2

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