如題,下面是一個用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