📄 contact.sh
字号:
#! /bin/sh#<Contacts> #Version:1.0#--Kay1531 Presents##define function below#directory="data" #get the number of lines in the documentcountLine() { read str<data if [ "$str" = "" ] then line=0 else line=`wc -l data` line=${line%" data"} fi return $line} #search by namesearch() { clear; echo "------------" echo "find a record" echo "------" echo "enter the name:" read name #find the record str=`grep -n /$name/ data` #get the number num=${str#*:'/'*'/'} num=${num%'/'} echo " " #already exists if [ "$str" != "" ] then echo "name: " $name echo "phone number: " $num #not exist,add the record else echo "record not found!" fi echo " "} #add a recordadd() { clear; echo "------------" echo "add a record" echo "------" echo "enter the name:" read name #find the record str=`grep -n /$name/ data` #get the number num=${str#*:'/'*'/'} num=${num%'/'} #get the sequence number of that record seq=${str%%':'*} #already exists if [ "$str" != "" ] then echo $name"("$num")"" already exits" echo "continue to edit or back? (y/n):" read yn #update the number if [ $yn = "y" ] then echo "enter the new number:" read num #get the number of total lines countLine line=$? #delete the previous record sed -i "$seq d" data #insert the present record echo $line" "$seq if [ $line -eq $seq ] then echo "/"$name"/"$num"/">>data else sed -i "$seq""i "/"$name"/"$num"/ data fi #back without change else return 0 fi #not exist,add the record else echo "enter the number:" read num #add the record countLine line=$? if [ $line -eq 0 ] then echo "/"$name"/"$num"/">data else echo "/"$name"/"$num"/">>data fi fi} #delete a record by namedelete() { clear; echo "------------" echo "delete a record" echo "------" echo "enter the name:" read name #find the record str=`grep -n /$name/ data` #get the number num=${str#*:'/'*'/'} num=${num%'/'} #get the sequence number of that record seq=${str%%':'*} #already exists if [ "$str" != "" ] then echo $name"("$num")"" already exits" echo "comfirm to delelte? (y/n):" read yn #delelte if [ $yn = "y" ] then echo "delete this record!" sed -i "$seq d" data #back without change else return 0 fi else echo"" echo "record not found!" echo"" fi} #list all the recordslist() { #get the number of lines countLine line=$? #list records echo " " if [ $line -ne 0 ] then echo "=Record List=" cat "data" | while read str do if [ $line -ne 0 ] then #get the number name=${str%'/'*'/'} name=${name#'/'} num=${str#'/'*'/'} num=${num%'/'} echo $name" "$num line=`expr $line - 1` fi done else echo "no record!" fi echo " "} #sort all the recordssort() { awk '{print $1,$2|"sort -k 1"}' data > temp #get the number of lines countLine line=$? #list records echo " " if [ $line -ne 0 ] then echo "=Record List=" cat "temp" | while read str do if [ $line -ne 0 ] then #get the number name=${str%'/'*'/'} name=${name#'/'} num=${str#'/'*'/'} num=${num%'/'} echo $name" "$num line=`expr $line - 1` fi done else echo "no record!" fi echo " "} #helphelp() { clear echo "==help==" echo ">search (f) input a name,search the record." echo ">add (a) input a name,add the record,if already exists,choose to modify or not." echo ">delete (d) input a name,delete the record." echo ">list (l) list all the records." echo ">sort (s) list all the records according to sequence of name." echo ">help (h) show help infomation." echo " "}##function defined above###main body##show welcoming infoop="y"while [ "$op" != "n" ]doclear;echo "----------------------------------------"echo "----------------------------------------"echo "<Contacts> Version:1.0"echo "--Kay1531 Presents";#choose menuecho "---------------"echo "==Menu=="echo ">search (f)"echo ">add (a)"echo ">delete (d)"echo ">list (l)"echo ">sort (s)"echo ">help (h)"echo "---------------"echo "Please select:"read menu;case $menu in "f")search;; "a")add;; "d")delete;; "l")list;; "s")sort;; "h")help;;esacecho "Continue or not(y/n): "read opdone
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -