免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3995 | 回复: 6

shell——cd唱片管理 [复制链接]

论坛徽章:
0
发表于 2011-06-21 12:42 |显示全部楼层
本帖最后由 青乡之b 于 2011-06-21 12:46 编辑
  1. #!/bin/sh
  2.   
  3. #A simple example shell script for managing a CD.



  4. #定义变量
  5. menu_choice=""
  6. current_cd=""
  7. title_file="title.cdb"
  8. tracks_file="tracks.cdb"
  9. trap 'rm -f $temp_file' EXIT
  10.   


  11. #定义工具型函数
  12. get_return(){
  13.    echo -e "Press return \c"
  14.    read x
  15.    return 0
  16. }
  17.   
  18. get_confirm(){
  19.    echo -e "Are you sure ? \c"
  20.      
  21.    while true
  22.    do
  23.       read x
  24.       case "$x" in
  25.            y | yes | Y | Yes |YES)
  26.              return 0;;
  27.            n | no | N | No | NO)
  28.              echo
  29.              echo "Cancel"
  30.              return 1;;
  31.            *) echo "Please enter yes or no:";;
  32.        esac
  33.    done
  34. }
  35.   


  36. #主菜单函数
  37. set_menu_choice(){
  38.    clear
  39.    echo "Options :-"
  40.    echo
  41.    echo "    a) Add new CD"
  42.    echo "    f) Find CD"
  43.    echo "    c) Count the CDs and tracks in the catalog"
  44.    if [ "$cdcatnum" != "" ]; then
  45.       echo " l) List tracks on $cdtitle"
  46.       echo " r) Remove $cdtitle"
  47.       echo " u) Update tracks information for $cdtitle"
  48.    fi
  49.    echo "    q) Quit
  50.    echo
  51.    echo -e "Please enter choice, then press return \c"
  52.    read menu_choice
  53.    return
  54. }
  55.   


  56. #插入标题文件和曲目文件函数
  57. insert_title(){
  58.    echo $* >> $title_file
  59.    return
  60. }
  61.   
  62. insert_track(){
  63.    echo $* >> $tracks_file
  64.    return
  65. }
  66.   
  67. add_record_tracks(){
  68.    echo "Enter track information for this CD"
  69.    echo "When no more tracks, enter q"
  70.    cdtrack=1
  71.    cdttitle=""
  72.    while [ "$cdttitle" != "q" ]
  73.    do
  74.       echo -e "Track $cdtrack, track title? \c"
  75.       read tmp
  76.       cdttitle=${tmp%%,*}
  77.       if [ "$tmp" != "$cdttitle" ]; then
  78.          echo "Sorry, no commas allowed"
  79.          continue
  80.       fi
  81.       if [ -n "$cdttitle" ]; then
  82.          if [ "$cdttitle" != "q" ]; then
  83.             insert_track $cdcatnum,$cdtrack,$cdttitle
  84.          fi
  85.       else
  86.          cdtrack=$((cdtrack-1))
  87.       fi
  88.       cdtrack=$((cdtrack+1))
  89.    done
  90. }

  91. add_records(){
  92.    echo -e "Enter catalog name\c"
  93.    read tmp
  94.    cdcatnum=${tmp%%,*}
  95.   
  96.    echo -e "Enter title\c"
  97.    read tmp
  98.    cdtitle=${tmp%%,*}
  99.   
  100.    echo -e "Enter type\c"
  101.    read tmp
  102.    cdtype=${tmp%%,*}
  103.   
  104.    echo -e "Enter artist/composer \c"
  105.    read tmp
  106.    cdac=${tmp%%,*}
  107.   
  108.    echo "About to add new entry"
  109.    echo "$cdcatnum $cdtitle $cdtype $cdac"
  110.      
  111.    if get_confirm; then
  112.       insert_title $cdcatnum,$cdtitle,$cdtype,$cdac
  113.       add_record_tracks
  114.    else
  115.       remove_records
  116.    fi
  117.      
  118.    return
  119. }
  120.   


  121. #查找唱片函数
  122. find_cd(){
  123.    if [ "$1" = "n" ]; then
  124.       asklist=n
  125.    else
  126.       asklist=y
  127.    fi
  128.    cdcatnum=""
  129.    echo -e "Enter a string to search for in the CD titles \c"
  130.    read searchstr
  131.    if [ "$searchstr" = "" ]; then
  132.       return 0
  133.    fi
  134.      
  135.    grep "$searchstr" $title_file > $temp_file
  136.    set $(wc -l $temp_file)
  137.    linesfound=$1
  138.      
  139.    case "$linesfound" in
  140.         0) echo "Sorry, nothing found"
  141.            get_return
  142.            return 0 ;;
  143.         1) ;;
  144.         2) echo "Sorry, not unique."
  145.            echo "Found the following"
  146.            cat $temp_file
  147.            get_return
  148.            return 0
  149.    esac
  150.      
  151.    IFS=","
  152.    read cdcatnum cdtitle cdtype cdac < $temp_file
  153.    IFS=" "
  154.      
  155.    if [ -z "$cdcatnum" ]; then
  156.       echo "Sorry, could not extract catalog field from $temp_file"
  157.       get_return
  158.       return 0
  159.    fi
  160.    
  161.    echo
  162.    echo catalog number: $cdcatnum
  163.    echo Title: $cdtitle
  164.    echo Type: $cdtype
  165.    echo Artist: $cdac
  166.    echo
  167.    get_return
  168.   
  169.    if [ "$asklist" = "y" ]; then
  170.       echo -e "View tracks for this CD ? \c"
  171.       read x
  172.       if [ "$x" ="y" ]; then
  173.          echo
  174.          list_tracks
  175.          echo
  176.       fi
  177.    fi
  178.    return 1
  179. }



  180. #更新唱片信息
  181. update_cd(){
  182.    if [ -z "$cdcatnum" ]; then
  183.       echo "You must select a CD first"
  184.       find_cd n
  185.    fi
  186.    if [ -n "$cdcatnum" ]; then
  187.       echo "Current tracks are: -"
  188.       list_tracks
  189.       echo
  190.       echo "This will re-enter teh tracks for $cdtitle"
  191.       get_confirm && {
  192.           grep -v "^${cdcatnum}," $tracks_file > $temp_file
  193.           mv $temp_file $tracks_file
  194.           echo
  195.           add_record_tracks
  196.       }
  197.    fi
  198.    return
  199. }



  200. #统计唱片数和曲目数
  201. count_cds(){
  202.    set $(wc -l $title_file)
  203.    num_titles=$1
  204.    set $(wc -l $tracks_file)
  205.    num_tracks=$1
  206.    echo found $num_titles CDs, with a total of $num_tracks tracks
  207.    get_return
  208.    return
  209. }



  210. #删除记录信息  
  211. remove_records(){
  212.    if [ -z "$cdcatnum" ]; then
  213.       echo You must select a CD first
  214.       find_cd n
  215.    fi
  216.    if [ -n "$cdcatnum" ]; then
  217.       echo "You are about to delete $cdtitle"
  218.       get_confirm &&{
  219.          grep -v "^${cdcatnum}," $title_file > $temp_file
  220.          mv $temp_file $title_file
  221.          grep -v "^${cdcatnum}," $tracks_file > $temp_file
  222.          mv $temp_file $tracks_file
  223.          cdcatnum=""
  224.          echo Entry removed
  225.       }
  226.       get_return
  227.    fi
  228.    return
  229. }



  230. #列出曲目  
  231. list_tracks(){
  232.    if [ "$cdcatnum" = "" ]; then
  233.       echo no CD selected yet
  234.       return
  235.    else
  236.       grep "^${cdcatnum}," $tracks_file > $temp_file
  237.       num_tracks=$(wc -l $temp_file)
  238.       if [ "$num_tracks" = "0" ]; then
  239.          echo no tracks found for $cdtitle
  240.       else {
  241.          echo
  242.          echo "$cdtitle: -"
  243.          echo
  244.          cut -f 2- -d , $temp_file
  245.          echo
  246.       } | ${PAGER: -more}
  247.       fi
  248.    fi
  249.    get_return
  250.    return
  251. }
  252.   



  253. #主程序部分  
  254. rm -f $temp_file
  255. if [ ! -f $title_file ]; then
  256.    touch $title_file
  257. fi
  258. if [ ! -f #tracks_file  ]; then
  259.    touch $tracks_file
  260. fi
  261.   
  262. clear
  263. echo
  264. echo
  265. echo "Mini CD manager"
  266. sleep 3
  267.   
  268. quit=n
  269. while [ "$quit" != "y" ];
  270. do
  271.    set_menu_choice
  272.    case "$menu_choice" in
  273.         a) add_records;;
  274.         r) remove_records;;
  275.         f) find_cd y;;
  276.         u) update_cd;;
  277.         c) count_cds;;
  278.         l) list_tracks;;
  279.         b)
  280.            echo
  281.            more $title_file
  282.            echo
  283.            get_return;;
  284.         q |Q) quit=y;;
  285.         *) echo "Sorry, choice not recognized";;
  286.     esac
  287. done
  288.   
  289. rm -f $temp_file
  290. echo "Finished"
  291. exit 0
复制代码
================================================================

运行脚本后,报错如下:
[root@GZH gzh]# sh cdmanager
cdmanager: line 287: unexpected EOF while looking for matching `"'
cdmanager: line 298: syntax error: unexpected end of file


帮忙看下?什么问题?(注意:  出错行cdmanager: line 287:——在倒数第二行!!)

论坛徽章:
0
发表于 2011-06-21 13:25 |显示全部楼层
回复 1# 青乡之b


    好像有两个地方:
  56行  echo "    q) Quit 是不是少了双引号,改成:echo "    q) Quit "
   285行 if [ ! -f #tracks_file  ]; then 错了,改成:if [ ! -f $tracks_file  ]; then

论坛徽章:
1
射手座
日期:2014-03-10 14:24:52
发表于 2011-06-21 14:11 |显示全部楼层
回复  青乡之b


    好像有两个地方:
  56行  echo "    q) Quit 是不是少了双引号,改成:echo "   ...
lionfun 发表于 2011-06-21 13:25



    太狠了!!!这都搞出来了

论坛徽章:
0
发表于 2011-06-22 13:23 |显示全部楼层
回复 2# lionfun


    我自己都看的眼睛花了,嫌麻烦~  
  这么多行难得你把它看完还找出错误
谢谢您!

论坛徽章:
0
发表于 2011-06-22 13:29 |显示全部楼层
回复 4# 青乡之b


    sh -x shellfile 调试 + vim 看脚本文字颜色 就很容易看出来了

论坛徽章:
20
CU大牛徽章
日期:2013-04-17 11:48:26羊年新春福章
日期:2015-03-10 22:39:202015年中国系统架构师大会
日期:2015-06-29 16:11:282015亚冠之平阳省
日期:2015-07-31 09:19:042015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-09-30 06:20:002015亚冠之柏太阳神
日期:2015-10-19 20:29:5915-16赛季CBA联赛之天津
日期:2016-11-29 14:03:4315-16赛季CBA联赛之北控
日期:2016-12-24 20:51:492015年辞旧岁徽章
日期:2015-03-03 16:54:15双鱼座
日期:2015-01-12 20:58:532014年中国系统架构师大会
日期:2014-10-14 15:59:00
发表于 2011-07-01 17:26 |显示全部楼层
LZ强人啊

论坛徽章:
0
发表于 2011-07-01 23:42 |显示全部楼层
这是linux程序设计里的吧
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP