An address book written in shell scripts

Source: Internet
Author: User

For example, the following is an address book written in a Linux Shell script. The "add, delete, and query" function has been implemented. The "change" function is complex and is to be continued ~~

 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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.