The final exercises in chapter II of the fourth edition of Linux programming

Source: Internet
Author: User

This article is the last exercise in chapter II of the fourth edition of Linux programming (2.8 integrated applications),
The title is to write a CD database application,

First:Save the stored data with two files, one is title_file.cdb, the other is column_file.cdb!
TITLE_FILE.CDB contains the basic information of the record: The following four fields:
CD catalog number, title, record type, composer or artist;
The column_file.cdb is a record that contains the song information: it has the following three fields:
CD catalog number, track number, song name

Second:
The following function modules are available:
Get_return () return function
Get_confirm () Continue Operation function
Add_Records () Add CD basic information function
Insert_title () Insert CD basic information function in TITLE_FILE.CDB
Add_records_cds () Add CD details function
Insert_track () Insert CD basic information function in COLUMN_FILE.CDB
Find_records () Find CD information function
Rm_records () Delete CD information function
Update_Records () Update CD details function
Count_records () Statistics CD number and number of songs information function
List_records () lists the details (songs) function of the selected CD
Memu_display () menu interface functions

The main commands used in the code are:
grep Filter every string of rows
Cut Cut an entire column string based on a field
wc Number of rows of statistics
Set
Read Keyboard input
Echo Output
>, >>, <; Input and output redirection
Rm
As well as various logic judgments, string manipulation, pipe command use, etc. ~

The topic is not difficult, mainly is familiar with various orders ~


The code is as follows:

#!/bin/bashtemp_file= "./tempfile.cdb" title_file= "./title_file.cdb" column_file= "./column_file.cdb" choose= "" # Returnget_return () {ECHO-E "press RETURN \c" read Xreturn 0} #confirmget_confirm () {echo] are u sure? ( y/n) "while true; Do read yncase ' $yn ' in y | Y) return 0;; n | N) return 1;; *) echo "Please enter Y or n";; Esacdone}add_records () {echo "Please input the record ' s name as this" echo "Cd123,cool Sax,jazz,bix" read Inputlocal temp= $ (Echo $input |egrep ' ^[[:alnum:]]+,[[:alpha:]]+,[[:alpha:]]+,[[:alpha:]]+$ ') if ["$temp" = = ""];thenecho "wrong name" Sleep 1return 0;elsecdnum=$ (echo $input |cut-d ', '-f1) cdtitle=$ (echo $input |cut-d ', '-f2 ') cdtype=$ (echo $input |cut-d ',  '-f3 ' cdac=$ (echo $input |cut-d ', '-f4) Fiecho "The name is $cdnum $cdtitle $cdtype $cdac" if get_confirm; theninsert_title $cdnum $cdtitle $cdtype $cdacadd _records_cdsreturnelse echo "You dont save the Record" Returnfi}add_records_cds () {echo "P Lease input The record ' s list name like this: "echo" Juhuatai,qianlizhiwai,... "reaD nameslocal k=$ (echo "$names" |awk-f ', ' {print NF} ') #echo $kdeclare-I j=1;y=$ ((k)); #echo $ywhile [$j-le $y];d oinser T_track $cdnum $j $ (echo "$names" |cut-d ', '-f $j) j= $j +1done}find_records () {if [["] = = [n"]]; thenasklist= "n"; elseas Klist= "Y"; #statementsfiecho "input a string to search for in the CD titles" read SEARCHSTR if ["$searchstr" = = ""];thenre    Turn 0;figrep-i "$searchstr" $title _file > $temp _file; #将计算出来的数目的第一个参数 (number of rows) assigned to Linesfoundset $ (wc-l $temp _file) linesfound=$1case "$linesfound" In0) echo "No found" get_ Returnreturn 0;; 1);; 2) echo "Sorry,many results:" Cat $temp _fileget_returnreturn 0; Esacread cdnum cdtitle cdtype CDAC < $temp _fileif [-Z "$cdnum"]; Thenecho "Sorry,can ' t extract details from $temp _file" Get_returnreturn 0#statementsfiechoecho "CD number: $cdnum" echo " Title: $cdtitle "echo" Type: $cdtype "echo" Artist: $cdac "echoget_returnif [[" $asklist "= =" Y "]]; Thenecho "View tracks for this CD?" Read Xif [["$x" = = "Y"]]; Thenecholist_records;echo#statementsfi #statementsfireturn 1}rm_records () {find_records n;if [[-N ' $cdnum "]] thenecho" Deleting CD: $cdnum $cdtit Le $cdtype $cdac "get_confirm &&{grep-v" ^ $cdnum "$title _file > $temp _filemv $temp _file $title _filegrep-v" ^$ Cdnum "$column _file > $temp _filemv $temp _file $column _filecdnum=" "echo" Delete Ready "}get_return# Statementsfireturn}update_records () {if ["$cdnum" = = ""];thenecho "you dont choose a CD" return; Fiecho "updateing the CD ' s songs $cdtitle "Get_confirm &&{grep-v" ^ $cdnum "$column _file > $temp _filemv $temp _file $column _fileechoadd_ Records_cds}return}count_records () {local num_cds=$ (wc-l $title _file|cut-d ""-f1) Local num_songs=$ (wc-l $column _file |cut-d "-f1" echo "The Nums for the CD was: $num _cds" echo "The Nums for the Songs is: $num _songs"}list_records () {["$c Dnum "= =" "]&&echo" no CD selected "&&returngrep" ^ $cdnum "$column _file > $temp _filelocal num_songs=$ ( Wc-l $temp _file) if [["$num _songs" = = "0"]]; TheneCho "No songs found for $cdtitle" Elseechoecho "$cdtitle:-" echocut-d ""-F 2-$temp _fileecho#statementsfiget_returnreturn  }memu_display () {echo "options:" Echo Echo "a) ADD CD" echo "f) Find CD" echo "R) Remove CD" echo "c) Count CDs" If ["$cdnum" ! = ""];thenecho "L) List tracks for $cdtitle" echo "u) Update track for $cdtitle" Fiecho "q) Quit ' echoread-p ' please Choos E which action: "Choosereturn}insert_title () {echo $* >> $title _filereturn}insert_track () {echo $* >> $column _filereturn} #mainrm-rf $temp _file[!-F $title _file] && touch $title _file; [!-f $column _file] && touch $column _file; sleep 1while truedo memu_displaycase "$choose" ina) add_records; R) Rm_records;; f) find_records y;; u) update_records;; c) count_records;; l) list_records;; b) Echomore $title _fileechoget_return;; q| Q) break;; *) echo "Sorry,choice no Right";; ESACDONERM-RF $temp _fileecho "finish~" Exit 0


The final exercises in chapter II of the fourth edition of Linux programming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.