用shell寫的一個電話簿

來源:互聯網
上載者:User

#! /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:a: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 then 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 "%-10s %s/n%-10s %s/n%-10s %s:/n%-10s %s/n/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 $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 "%-10s %s/n%-10s %s/n%-10s %s/n%-10s %s/n/n",/<br />"NAME :",$1,"PHONE :",$2,"ADDRESS :",$3,"EMAIL :",$4;<br />}'<br />#exit $?

 

 

這些指令碼不難會點的應該都能看懂

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.