linux实习 数据管理程序(1)

我院的linux实习,具体实习要求如下:

ß以个人的CD唱片库为例,CD信息保存在文本文件中,设计程序实现对CD信息的显示、输入、更新、检索、删除、使用统计等功能。CD唱片信息包括:CD唱片的目录编号、标题、曲目类型、作曲家或艺术家、曲目编号、曲名等。

也提供了相应的学习资料,linux程序设计第四版,封面是这样



啊,shell下实现因为并不是很熟练,所以搜索到以下程序:

#!/bin/bash  
# this program aims to manage CD and songs contained in the CD  
#   history:  
#           2014-10-14  first release  
# manageCd.sh  
mainBoard()  
{  
  choice=1  
until [ $choice == "q" ]  
do  
    clear  
    echo    "options as follow:"  
    echo "a) Add new CD"  
    echo "v) View CD"  
    echo "d) Delete CD"  
    echo "f) Find cd"  
    echo "c) Count the CD and songs in the catalog"  
        echo "u) update CD"  
    echo "q) quit"  
    echo ""  
  
    read -p "please input your choice: " choice  
  while [ "$choice" != "a" -a "$choice" != "v" -a "$choice" != "d" -a "$choice" != "f" -a "$choice" != "c" -a "$choice" != "u" -a "$choice" != "q" ]  
  do  
    read -p "input error,please input again([a,v,d,f,c,u]):" choice  
  done  
  
  case $choice in  
              "a")  
         addNewCD  
         ;;  
  
              "v")  
         viewCD  
         ;;  
  
              "d")  
         deleteCD  
         ;;  
  
          "f")  
                 findCD  
         ;;  
              
              "c")  
         countCDAndSongs  
         ;;  
  
              "u")  
         updateCD  
     ;;  
  
     "*")  
     echo "error"  
     ;;  
  esac  
  echo -n "press to return"  
  read x  
done  
}  
  
addNewCD()  
{  
    title_file="title_file"  
  songs_file="songs_file"  
   
     
  read -p "Enter CD name: " name  
  while true  
   do  
   flag=0  
    for x in `awk -F , '{print $1}' $title_file`  
     do  
       if [ "$name" == "$x" ]; then  
        echo -n "CD name:$name has existed,please input CD name again: "   
        read name  
        flag=1  
        break;  
       fi  
     done  
      
    if [ "$flag" == 0 ]; then  
     break;  
    fi  
   done  
  
    
    
  read -p "Enter CD Title: " title  
  read -p "Enter CD Type: " type  
  read -p "Enter The artist/composer: " composer  
  
  echo "About to add new entry"  
  echo  "$name  $title  $type  $composer"  
  read -p "Are you sure to add?(Y|y|N|n): " choice  
    
  while [ $choice != "y" -a $choice != "Y" -a $choice != "N" -a $choice != "n" ]  
  do  
        read -p "input error! please input again(Y,y,N,n): " choice  
  done    
   
  if [ $choice == "n" ]; then   
    echo "add cancelled"  
    return 1  
  fi  
  
  if [ -f $title_file ]; then  
      flag=1  
  else  
     flag=0  
  fi  
    
  if [ $flag == "1" ]; then  #file exists  
      echo "$name, $title, $type, $type, $composer" >> $title_file  
  else   
    touch $title_file    #create file  
    echo "$name, $title, $type, $type, $composer" > $title_file  
  fi  
      
  num=1  
  echo "Enter songs information for this CD:$name"  
  echo "When no more songs enter q"  
  while true  
  do  
    echo -n "song $num: song's name: "   
     
    while true  
     do  
        read songsname  
      if [ "$songsname" == "" ]; then  
        read -p "empty name,please input song's name again: " songsname  
      else  
       break;  
      fi  
    done  
     
    if [ "$songsname" == "q" ]; then   
      echo "add new CD finished! as follows: "  
      viewCD  
      return 0  
    fi  
  
    [ ! -f "$songs_file" ] && echo > "$songs_file" #to create empty file  
    echo "$name,$num,$songsname" >> "$songs_file"  
    num=`expr $num + 1`   # add 1  
  done    
    
}  
  
viewCD()  
{  
        songs_file="songs_file"  
    title_file="title_file"  
    echo "file $title_file: "  
    
  if test -f "$title_file"; then  
     cat "$title_file" | more  
  else  
     echo "empty"  
     return 1  
  fi  
  
  echo ""  
  
  echo "file $songs_file: "  
  [ -f "$songs_file" ] && cat "$songs_file" | more  
  [ ! -f "$songs_file" ] && echo "empty" && return 1  
  echo ""  
  
 #awk -F , '{print $1}' "$title_file" #get the first row seperated by ,  
     
  for x in `awk -F , '{print $1}' "$title_file"`  
   do  
     echo "$x: " && grep "$x" "$songs_file"  
     echo ""  
   done  
  
 # read -p "input to return" abc  
  return 0  
}  
  
deleteCD()  
{  
    
  title_file="title_file"  
  songs_file="songs_file"  
  read -p "please input CD name: " name  
  while [ "$name" == "" ]   
  do  
   read -p "no input,please input again: " name  
  done  
    
  echo "will delete CD $name..."  
  getConfirm &&  
  {  
   if grep "$name" $title_file; then  
       
     grep -v "$name" $title_file > tmpfile  
     mv tmpfile $title_file  
     grep -v "$name" $songs_file > tmpfile  
     mv tmpfile $songs_file  
     echo "delete CD name:$name finished!"  
     echo "CD contents:"  
     viewCD  
     #viewSongs  
   fi  
  }  
  return 0  
}  
  
findCD()  
{  
   title_file="title_file"  
   songs_file="songs_file"  
   echo -n "please input CD name which you want to find: "  
   read cdname  
   while true  
    do  
      if [ "$cdname" == "" ]; then  
        echo -n "your input is empty,please input cd name again: "  
        read cdname  
      else  
        break  
      fi  
    done  
      
    [ ! -f "$title_file" ] && echo "no this cd: $cdname" && return 1  
  
    flag=0  
    for x in `awk -F , '{print $1}' $title_file`  
     do  
    if [ "$cdname" == "$x" ]; then  
         flag=1  
         grep "$cdname" $title_file  
         echo "songs in $cdname: "  
         [ -f "$songs_file" ] && grep "$cdname" $songs_file && flag=2  
         [ ! -f "$songs_file" ] && echo "this cd has no songs" && flag=3  
        # [ ! grep "$cdname" $songs_file ] && echo "this cd has no songs" && flag=3  
         break;  
        fi  
     done  
      
    if [ "$flag" == "0" ]; then  
    echo "no this cd: $cdname"  
    fi  
      
}  
  
getConfirm()  
{  
  echo -n "Are you sure?"   
    while true  
  do  
     read x  
    case "$x" in  
     y|Y|yes|YES|Yes)  
       return 0;;  
  
     n|N|no|NO|No)  
       echo "cancelled"  
       return 1;;  
  
      *)  
       echo "please input yes or no";;  
   esac   
  done  
}  
  
countCDAndSongs()  
{  
   title_file="title_file"  
   songs_file="songs_file"  
     
   cdnum=0  
   songnum=0  
   for x in `awk -F , '{print $1}' $title_file`  
    do  
      cdnum=`expr $cdnum + 1` # add 1       
    done  
  
   for x in `awk -F , '{print $1}' $songs_file`  
    do  
      songnum=`expr $songnum + 1` # add 1  
    done  
     
   echo "find $cdnum CDs,with a total of $songnum songs"  
   return 0  
     
}  
  
updateCD()  
{  
   title_file="title_file"  
   songs_file="songs_file"  
   read -p "please input the CD name you want to update: " cdname  
   while true  
    do  
      if [ "$cdname" == "" ]; then  
        echo -n "empty input, please input again: "   
        read cdname  
      else  
        break  
      fi  
    done  
      
   flag=0   
   for x in `awk -F , '{print $1}' $title_file`  
    do  
      if [ "$cdname" == "$x" ]; then  
       flag=1  
       echo "this cd: "  
       echo " cdname   title    type    composer"  
       grep "$cdname" $title_file  
         
       echo "press enter to not update"   
  
       read -p "update the title?" cdtitle  
       read -p "update the type?"  cdtype  
       read -p "update the composer?" cdartist  
       if [ "$cdtitle" == "" -o "$cdtype" == "" -o "$cdartist" == "" ]; then  
         if [ "$cdtitle" == "" ]; then  
           cdtitle=`grep "$cdname" $title_file | awk -F , '{print $2}'`  
         fi  
   
         if [ "$cdtype" == "" ]; then  
           cdtype=`grep "$cdname" $title_file | awk -F , '{print $3}'`  
         fi  
  
         if [ "$cdartist" == "" ]; then  
           cdartist=`grep "$cdname" $title_file | awk -F , '{print $4}'`  
         fi  
       fi  
            
       grep -v "$cdname" $title_file > tmpfile  
       echo "$cdname,$cdtitle,$cdtype,$cdartist" >> tmpfile  
       mv tmpfile $title_file  
       echo "update finished! as follows: "  
       cat $title_file | more  
       break  
     fi  
    done  
      
    if [ "$flag" == "0" ]; then  
      echo "no this cd: $cdname"  
    fi  
  
  
}  
mainBoard

shell小程序,运行结果如下:


今天粗略的学习了下dialog的使用,决定进行实践

考试刚考完让自己放松下~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值