用shell指令碼編寫的一個通訊錄

來源:互聯網
上載者:User

  如題,下面是一個用linux shell指令碼編寫的通訊錄,已實現了“增、刪、查”功能,“改”功能比較複雜,待續~~

 1 #!/dev/bash 2  3 # Name of address book 4 BOOK="address-book.txt" 5  6 exit=0 7  8 add() { 9     # Ask the user for a name and assign to a variable10     echo -n "Name of person: " 11     read name12 13     # Ask the user for a phone number and assign to a variable14     echo -n "Phone number: "15     read phone16 17     # Echo the answers and ask for confirmation18     echo "Should I enter the values:"19     echo -e "$name ; $phone \n"20     echo -n "y/n: "21     read answer22 23     if [ "$answer" == "y" ] 24     then25     # Write the values to the address book26     echo "$name ; $phone" >>$BOOK27     else28     # Give the user a message29     echo "$name ; $phone NOT written to $BOOK"30     fi31 }32 33 34 list() {35     # Print the book with line numbers and paused with less36     nl --number-separator=":    " $BOOK | less 37 }38 39 find() {40     # Ask the user what to look for.41     echo -n "What person or number are you seeking: "42     read find43 44     # Print the header before the answer45     echo "Name ; Phone number"46     grep -i $find $BOOK47 }48 49 del() {50     # Ask the user which line to delete51     echo -n "Which line should I delete: "52     read number53 54     # Rename the file before deleting55     mv $BOOK boo.txt56 57     # Add line numbers and delete against that number58     nl --number-separator=":" boo.txt | grep -v $number: | awk -F: '{print $2}' |  tee $BOOK59 }60 61 62 main() {63     while [ $exit -ne 1 ]64     do65     echo "What operation do you want?"66     echo -e "add, list, find, del, exit: "67     read answer68 69     if [ "$answer" = "add" ]70     then71         add72     elif [ "$answer" = "list" ]73     then74         list75     elif [ "$answer" = "find" ]76     then77         find78     elif [ "$answer" = "del" ]79     then80         del81     elif [ "$answer" = "exit" ]82     then83         exit=184     else85         echo "I do not understand the command."86     fi87     done88     exit 089 }90 main

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.