新人学习shell_简单的CD管理系统

作者在 2013-08-02 20:35:05 发布以下内容

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

 

                     开始学习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号码,曲目号码——每张CD1开始,曲目名称

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 #变量cdcatnumnull吗,是返回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

进程间通信 | 阅读 2360 次
文章评论,共0条
游客请输入验证码
最新评论