#! /Bin/bash <br/> # script filename: addbook. bsh <br/> # comment: add info to datafile <br/> # datafile: Conn. dat <br/> # initialize the variables <br/> datafile = "Conn. dat "<br/> name =" "<br/> phone =" "<br/> address =" "<br/> email =" "<br/> # Check in argument list then add info to datafile <br/> If [$ #-LT 1]; then <br/> echo "input some info by prompt to add! "<Br/> echo" name: "; read name <br/> echo" Phone: "; read phone <br/> echo" Address :"; read address <br/> echo "Email:"; read email <br/> else <br/> while getopts N: P:: E Option <br/> DO <br/> case $ option in <br/> N) <br/> name = $ option <br/> ;; <br/> P) <br/> phone = $ option <br/>; <br/> A) <br/> address = $ option <br/> ;; <br/> E) <br/> email = $ option <br/> ;; <br/> esac <br/> done <br/> fi <br/> # in the end of file to add N show all info <br/> echo "$ name: $ Phone: $ address: $ email" >>$ datafile <br/>. showbook. bsh Conn. dat <br/> # exit $?
#! /Bin/bash <br/> # Script Name: deletebook. bsh <br/> # comment: this script use Delete what you specify field info <br/> usage = "Usage: 'basename $ 0' [field] "<br/> mydatafile = $ home/Shell/telephone-book/Conn. dat <br/> searchfield = "$1" <br/> tmpf1 = "$ PWD/update. $ "<br/> tmpf2 =" $ PWD/Delete. $ "<br/> Doclean () <br/>{< br/> RM-F $ tmpf1 $ tmpf2 $ tmpf1.new 2>/dev/null <br/>}< br/> failed () <br/>{< br/> If ["$1"-Ne 0]; Then <br/> shift <br/> Print $ @ <br/> Doclean # Is do clean function <br/> Exit 1 <br/> fi <br/>} <br/> CP $ mydatafile $ tmpf1 <br/> grep-I "$ searchfield" "$ tmpf1"> "$ tmpf2" 2>/dev/null <br/> failed $? "No matches found" <br/> exec 5 <"$ tmpf2" <br/> while read line <& 5 <br/> DO <br/> echo "$ line" | <br/> awk-F: '{<br/> printf "%-10 S % s/n %-10 S % s/n %-10 S % s: /n %-10 S % s/n ",/<br/>" name: ", $1," Phone: ", $2," Address :", $3, "Email:", $4; <br/>}' <br/> echo "do you want Delete this line! (Y/n) "<br/> Read flag </P> <p> case $ flag in <br/> [YY] | [YY] [es]) <br/> # echo "deleting... "<br/> grep-IV" $ line "" $ tmpf1 ">" $ tmpf1.new "<br/> failed $? "Unable update data" <br/> mv "$ tmpf1.new" "$ tmpf1" <br/> failed $? "Unable update data" <br/> # echo "finish" <br/>;< br/> *) <br/> echo "not delete this line" <br/> ;; <br/> esac </P> <p> done <br/> exec 5 <&-<br/> MV $ mydatafile. bak 2>/dev/null <br/> failed $? "Unable update data" <br/> MV $ tmpf1 $ mydatafile 2>/dev/null <br/> failed $? "Unable update data" <br/> Doclean <br/> # exit $?
#! /Bin/bash <br/> # Script Name: showbook. bsh <br/> # comment: If $1 is empty will show all else use search field find line and show it. <br/> # datafile: Conn. dat <br/> searchfield = "$1" <br/> mydatafile = "$ home/Shell/telephone-book/Conn. dat "<br/> If [! -F "$ mydatafile"]; then <br/> echo "$ mydatafile is not exist or is not a file"> & 2 <br/> Exit 1 <br/> fi <br/> grep "$ searchfield "" $ mydatafile "| <br/> awk-F: '{<br/> printf "%-10 S % s/n %-10 S % s/n %-10 S % s/n %-10 S % s/n /n ", /<br/> "name:", $1, "Phone:", $2, "Address:", $3, "Email:", $4; <br/>} '<br/> # exit $?
These scripts are easy to understand.