加多CD——a)增添CD的同期给CD扩充对应的戏码音讯葡京网站spj3355

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

                     领头读书shell 脚本的 应用比如

 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

简易的CD管理体系

完成效果与利益:  加多CD——a)扩充CD的同失常间给CD扩大对应的曲目消息

                     删除CD——r)删除钦赐的CD

                     查找CD——f)查找钦命的CD

                    
更新CD——u)更新钦赐CD中的曲目音信——重新给该CD加多曲目音讯

                     显示CD——l)展现当前线指挥部定CD的曲目新闻

                     总结CD——c)总计有多少张CD,共有多少曲目

                     退出CD——q)退出CD管理连串

 

代码:

============================= 代码早先 ===============================

#!/bin/bash

 

 

#### global variable define strat ####

menu_choice=””

current_cd=””

title_file=”title.cdb” 
#保存CD的信息,包括CD号码,CD标题,CD类型,CD作者

tracks_file=”tracks.cdb”

#封存全体曲目音信,满含CD号码,曲目号码——每张CD由1开头,曲目名称

temp_file=/tmp/cdb.$$ #有时文件

trap `rm -rf $temp_file` EXIT #境遇抛锚时去除一时文件

#### global variable define end ####

 

 

 

#### function define about tools start ####

get_return() #回到值函数,须求客商输入供给的重返值,0平常,非0卓殊

{

       echo -e “Press return: c” #体现字符串消息

       read x #给变量x读入三个值

       return 0

}

 

get_confirm() #认可函数,供给顾客输入yes只怕no大概y或许n

{

       echo -e “Are you sure? c”

       while true

       do

              read x

              case “$x” in

                     y | yes | Y | Yes | YES) return 0;;

                     n | no | N | No | NO) echo

                                         echo “Cancelled”

                                         return 1;;

                     *) echo -e “Please enter yes or no: c”;;

              esac

       done

}

#### function define about tools end ####

 

 

 

 

#### set menu chocie function start ####

set_menu_choice() #每趟操作选项的浮现

{

#     clear

       echo “Options :-“

       echo

       echo ”     a) Add new CD”

       echo ”     f) Find CD”

       echo ”     c) Count the CDs and tracks in the catalog”

       if [ “$cdcatnum” != “” ];then #时下条件有对CD的操作

              echo ”     l) List tracks on $cdtitle”

              echo ”     r) Remove $cdtitle”

              echo ”     u) Update track information for $cdtitle”

       fi

       echo ”     q) Quit”

       echo

       echo -e “Please enter choice the press return: c”

       read menu_choice #读入客商输入的操作选项

       return

}

#### set menu chocie function end ####

 

 

#### insert and add opration start  ####

insert_title()

{

       echo $* >> $title_file #将该函数的全体参数输入到文件

       return

}

 

insert_track()

{

       echo $* >> $tracks_file

       return

}

 

add_record_tracks()

{

       echo “When no more tracks enter q”

       echo “Enter track information for this CD”

       cdtrack=1

       cdttitle=””

       while [ “$cdttitle” != “q” ]

       do

              echo -e “Track $cdtrack, track title? c”

              read tmp

              cdttitle=${tmp%%,*} #不可能输入含有逗号的曲目名称

              if [ “$tmp” != “$cdttitle” ]; then

                     echo “Sorry, no commas allowed”

                     continue

              fi

              if [ -n “$cdttitle” ]; then

                     if [ “$cdttitle” != “q” ]; then

                            insert_track $cdcatnum,$cdtrack,$cdttitle
#调用函数,插入曲目消息

                     fi

              else

                     cdtrack=$((cdtrack-1))
#当为q时,上次运营已经加1,所以做减风华正茂操作

              fi

              cdtrack=$((cdtrack+1)) #曲目号码自动抬高

       done

}

 

add_records()

{

       #Prompt for the initial infomation

      

       echo -e “Enter catalog name: c”

       read tmp

       cdcatnum=${tmp%%,*}

      

       echo -e “Enter title: c”

       read tmp

       cdtitle=${tmp%%,*}

      

       echo -e “Enter type: c”

       read tmp

       cdtype=${tmp%%,*}

 

       echo -e “Enter arrist or composer: c”

       read tmp

       cdac=${tmp%%,*}

 

       #Check that they want to enter the infomation    

       echo “About to add new entry”

       echo “$cdcatnum, $cdtitle, $cdtype, $cdac” #来得变量的值的措施

 

       #If confirmed then append it to the titles file

       if get_confirm; then

              insert_title $cdcatnum,$cdtitle,$cdtype,$cdac
#添加CD信息

              add_record_tracks
#给该CD增多曲目信息——必要一次性拉长达成

       else

              remove_records #调用删除函数

       fi

 

       return

}

#### insert and add opration end  ####

 

 

 

 

#### list track start ####

list_tracks()

{

        if [ “$cdcatnum” = “” ]; then

                echo “no CD selected yet”

                return

        else

                grep “^${cdcatnum},” $tracks_file > $temp_file

 #在曲目文件中搜寻以CD号码最先以逗号甘休的戏码音讯,并将非此音信输出到不时文件

                set $(wc -l $temp_file)
#设置职责参数为总计曲指标个数新闻

                num_tracks=$1 #即该职位参数是曲指标个数

              if [ “num_tracks” = “0” ]; then

                        echo “no tracks found for $cdtitle”

                else {

                        echo

                        echo “$cdtitle :-“

                        echo

                        cut -f 2- -d , $temp_file

#出口不经常文件中曲目标后两项音讯,即曲目号码,曲目名称

                        echo

              } | ${PAGER:-more} #分页展现

                fi

        fi

        get_return

        return

}

#### list tracks end ####

 

 

 

 

 

#### find CD start  ####

find_cd() #该函数须求一个参数,表示是还是不是出示查询到的CD的曲目音信

{

       if [ “$1” = “n” ]; then

              asklist=n #不显示

       else

              asklist=y #显示

       fi

       cdcatnum=””

       echo -e “Enter a string to search for in the CD titles: c”

       read searchstr

       if [ “searchstr” = “” ]; then

              return 0

       fi

 

       grep “$searchstr” $title_file > $temp_file

#在CD文件中寻觅钦定题指标新闻存款和储蓄到有时文件中

      

       set $(wc -l $temp_file)

       linesfound=$1

 

       case “$linesfound” in

              0)    echo “Sory, nothing found”

                     get_return

                     return 0

                     ;;

              1)    ;;

              2)    echo “Sorry, not unique”

                     echo “Found the following”

                     cat $temp_file

                     get_return

                     return 0

       esac

 

       IFS=”,” #修改默认选项,改为以逗号为分隔符

       read cdcatnum cdtitle cdtype cdac < $temp_file

#从有的时候文件以逗号为分隔符一次读入需求的CD信息选项

       IFS=” ” #复原默许设置

 

       if [ -z “$cdcatnum” ]; then 
#变量cdcatnum是否是0,是0时返回true

              echo “Sorry, could not extract catalog field from
$temp_file”

              get_return

              return 0

       fi

 

       echo

       echo “Catalog number: $cdcatnum”

       echo “Title: $cdtitle”

       echo “Type: $cdtype”

       echo “Artist or Composer: $cdac”

       echo

       get_return

 

       if [ “$asklist” = “y” ]; then

              echo -e “View tracks for this CD? c”

              read x

              if [ “$x” = “y” ]; then

                     echo

                     list_tracks

                     echo

              fi

       fi

       return 1

}

#### find CD end ####

 

 

 

 

#### update CD start ####

update_cd()

{

       if [ -z “$cdcatnum” ]; then

              echo “You must select a CD first”

              find_cd n

       fi

       if [ -n “$cdcatnum” ]; then #变量cdcatnum非null吗,是返回true

              echo “Current tracks are :-“

              list_tracks

              echo

              echo “This will re-enter the tracks for $cdtitle”

              get_confirm && {

                                   grep -v “^${cdcatnum},” $tracks_file
> $temp_file

                                   mv $temp_file $tracks_file
#剔除无需的戏码音讯,并改善曲目文件

                                   echo

                                   add_record_tracks #履新曲目新闻

                             }

              fi

              return

}

#### update CD end ####

 

 

 

#### count CD start ####

count_cds()

{

       set $(wc -l $title_file)  #统计CD的个数

       num_titles=$1

       set $(wc -l $tracks_file) #总结曲指标个数

       num_tracks=$1

       echo “Found $num_titles CDs, with a total of $num_tracks
tracks”

       get_return

       return

}

#### count CD end ####

 

 

 

#### remove records start ####

remove_records()

{

       if [ -z “$cdcatnum” ]; then

              echo “You must select a CD first”

              find_cd n

       fi

       if [ -n “$cdcatnum” ]; then

              echo “You are about to delete $cdtitle”

              get_confirm && {

                                   grep -v “^${cdcatnum},” $title_file
> $temp_file

                                   mv $temp_file $title_file
#除去CD文件中无需的CD音信

                                   grep -v “^${cdcatnum},” $tracks_file
> $temp_file

                                   mv $temp_file $tracks_file
#删除曲目文件中无需的戏码音信

                                   cdcatnum=””

                                   echo “Entey removed”

                            }

       fi

       return

}

#### remove records end ####

 

 

 

#### main start ####

 

rm -rf $temp_file #先删除有的时候文件

if [ ! -f $title_file ]; then #认清CD文件非常常文书呢?是回到true

       touch $title_file #创建该CD文件

fi

if [ ! -f $tracks_file ]; then #同理剖断曲目文件

       touch $tracks_file

fi

 

#Now the application proper

echo

echo

echo “Mini CD manager”

sleep 1

 

quit=n

while [ “$quit” != “y” ]

do

       set_menu_choice #显示选项操作项,并给变量menu_choice读入值

       case “$menu_choice” in

              a)    add_records;;

              r)     remove_records;;

              f)     find_cd y;;

              u)    update_cd;;

              c)    count_cds;;

              l)     list_tracks;;

              b)

                     echo

                     more $title_file

                     echo

                     get_return;;

              q | Q)      quit=y;;

              *)    echo “Sorry, choice not recognized”;;

       esac

done

 

#Tidy up and leave

rm -f $temp_file  #去除一时文件

echo “Finished”

exit 0

#### main end ####

 

=============================== 代码截至 ==============================

 

 

 

 

运作结果:

[root@localhost shell]# chmod +x cdmanager.sh   //给shell增添实施权限

[root@localhost shell]# ./cdmanager.sh          //运行该shell

 

 

Mini CD manager

Options :-

 

        a) Add new CD

        f) Find CD

        c) Count the CDs and tracks in the catalog

        q) Quit

 

Please enter choice the press return: a——输入a表示增多CD

Enter catalog name: CD88——输入CD88表示CD的编号

Enter title: myCD88——输入myCD88 表示CD88的标题

Enter type: liu_xing——输入liu_xing 表示CD88上的曲目类型

Enter arrist or composer: zhou_jie_lun——输入zhou_jie_lun
表示CD88的作曲家

About to add new entry

CD88, myCD88, liu_xing, zhou_jie_lun——彰显增加消息,以便确认

Are you sure? y ——输入y表示确定精确

When no more tracks enter q

Enter track information for this CD

Track 1, track title? ju_hua_tai——输入CD88的曲目

Track 2, track title? huang_jin_jia——输入CD88的曲目

Track 3, track title? qian_li_zhi_wai——输入CD88的曲目

Track 4, track title? shuan_jie_gun——输入CD88的曲目

Track 5, track title? dong_feng_po——输入CD88的曲目

Track 6, track title? q——退出该CD88曲指标录入

Options :-

 

        a) Add new CD

        f) Find CD

        c) Count the CDs and tracks in the catalog

        l) List tracks on myCD88

        r) Remove myCD88

        u) Update track information for myCD88

        q) Quit

 

Please enter choice the press return: l——输入l,展现当前CD的戏码音信

 

myCD88 :-

 

1,ju_hua_tai

2,huang_jin_jia

3,qian_li_zhi_wai

4,shuan_jie_gun

5,dong_feng_po

 

Press return: 0——输入三个重临值,0代表平常,非0表示格外

Options :-

 

        a) Add new CD

        f) Find CD

        c) Count the CDs and tracks in the catalog

        l) List tracks on myCD88

        r) Remove myCD88

        u) Update track information for myCD88

        q) Quit

 

Please enter choice the press return: c——总计CD总的数量和曲目总量

Found 1 CDs, with a total of 5 tracks

Press return: 0——输入一个再次回到值,0意味平常,非0表示非常

Options :-

 

        a) Add new CD

        f) Find CD

        c) Count the CDs and tracks in the catalog

        l) List tracks on myCD88

        r) Remove myCD88

        u) Update track information for myCD88

        q) Quit

 

Please enter choice the press return: a——添加CD信息

Enter catalog name: CD77

Enter title: myCD77

Enter type: gu_dian

Enter arrist or composer: yue_guang

About to add new entry

CD77, myCD77, gu_dian, yue_guang

Are you sure? q——输入参数错误

Please enter yes or no: yes——应该输入为yes恐怕no

When no more tracks enter q

Enter track information for this CD

Track 1, track title? yue_guang——加多当前CD的戏码消息

Track 2, track title? q——曲目消息增添甘休

Options :-

 

        a) Add new CD

        f) Find CD

        c) Count the CDs and tracks in the catalog

        l) List tracks on myCD77

        r) Remove myCD77

        u) Update track information for myCD77

        q) Quit

 

Please enter choice the press return: c——总计CD总个数和曲目总量

Found 2 CDs, with a total of 6 tracks

Press return: 0

Options :-

 

        a) Add new CD

        f) Find CD

        c) Count the CDs and tracks in the catalog

        l) List tracks on myCD77

        r) Remove myCD77

        u) Update track information for myCD77

        q) Quit

 

Please enter choice the press return: f——查找钦命CD标题标CD

Enter a string to search for in the CD titles:
myCD88——指定的CD标题为myCD88

 

Catalog number: CD88 ——显示该CD的信息

Title: myCD88

Type: liu_xing

Artist or Composer: zhou_jie_lun

 

Press return: 0

View tracks for this CD? y——显示该CD的戏码信息吗

 

 

myCD88 :-  ——该CD曲目信息的体现

 

1,ju_hua_tai

2,huang_jin_jia

3,qian_li_zhi_wai

4,shuan_jie_gun

5,dong_feng_po

 

Press return: 0

 

Options :-

 

        a) Add new CD

        f) Find CD

        c) Count the CDs and tracks in the catalog

        l) List tracks on myCD88

        r) Remove myCD88

        u) Update track information for myCD88

        q) Quit

 

Please enter choice the press return: l——展现当前CD的戏码音信

 

myCD88 :-

 

1,ju_hua_tai

2,huang_jin_jia

3,qian_li_zhi_wai

4,shuan_jie_gun

5,dong_feng_po

 

Press return: 0

Options :-

 

        a) Add new CD

        f) Find CD

        c) Count the CDs and tracks in the catalog

        l) List tracks on myCD88

        r) Remove myCD88

        u) Update track information for myCD88

        q) Quit

 

Please enter choice the press return: u——更新当前的CD曲目新闻

Current tracks are :-

 

myCD88 :-

 

1,ju_hua_tai

2,huang_jin_jia

3,qian_li_zhi_wai

4,shuan_jie_gun

5,dong_feng_po

 

Press return: 0

 

This will re-enter the tracks for myCD88

Are you sure? n——确认是否更新该CD

 

Cancelled

Options :-

 

        a) Add new CD

        f) Find CD

        c) Count the CDs and tracks in the catalog

        l) List tracks on myCD88

        r) Remove myCD88

        u) Update track information for myCD88

        q) Quit

 

Please enter choice the press return: f

Enter a string to search for in the CD titles: myCD77

 

Catalog number: CD77

Title: myCD77

Type: gu_dian

Artist or Composer: yue_guang

 

Press return: 0

View tracks for this CD? n——展现该CD的曲目音信呢

Options :-

 

        a) Add new CD

        f) Find CD

        c) Count the CDs and tracks in the catalog

        l) List tracks on myCD77

        r) Remove myCD77

        u) Update track information for myCD77

        q) Quit

 

Please enter choice the press return: u

Current tracks are :-

 

myCD77 :-

 

1,yue_guang

 

Press return: 0

 

This will re-enter the tracks for myCD77

Are you sure? y——确认更新该CD的戏码音讯(相当于该CD的曲目音讯从新输入)

 

When no more tracks enter q

Enter track information for this CD

Track 1, track title? bei_duo_fen

Track 2, track title? shu_bo_te

Track 3, track title? q

Options :-

 

        a) Add new CD

        f) Find CD

        c) Count the CDs and tracks in the catalog

        l) List tracks on myCD77

        r) Remove myCD77

        u) Update track information for myCD77

        q) Quit

 

Please enter choice the press return: l

 

myCD77 :-

 

1,bei_duo_fen

2,shu_bo_te

 

Press return: 0

Options :-

 

        a) Add new CD

        f) Find CD

        c) Count the CDs and tracks in the catalog

        l) List tracks on myCD77

        r) Remove myCD77

        u) Update track information for myCD77

        q) Quit

 

Please enter choice the press return: r——删除当前的CD

You are about to delete myCD77

Are you sure? y——确认删除当前CD

Entey removed

Options :-

 

        a) Add new CD

        f) Find CD

        c) Count the CDs and tracks in the catalog

        q) Quit

 

Please enter choice the press return: f

Enter a string to search for