2016-05-07 28 views
1

Hey, ich habe versucht, ein einfaches Bibliotheksverwaltungssystem mit Shell Script als Teil meiner Unix-UNIX-Zuordnung zu simulieren, aber ich habe seltsame Fehler im Skript.Bibliotheksverwaltung mit Shell Script

Hier ist mein Skript

menu_choice="" 
record_file="bookRecords.ldb" 
temp_file=/tmp/ldb.$$ 
trap 'rm -f $temp_file' EXIT 


get_return(){ 
printf '\tPress return\n' 
read x 
return 0 
} 
  
get_confirm(){ 
printf '\tAre you sure?\n' 
while true 
do 
  read x 
  case "$x" in 
      y|yes|Y|Yes|YES) 
     return 0;; 
      n|no|N|No|NO) 
          printf '\ncancelled\n' 
          return 1;; 
      *) printf 'Please enter yes or no';; 
  esac 
done 
} 

set_menu_choice(){ 
clear 
printf 'Options:-' 
printf '\n' 
printf '\ta) Add new Books records\n' 
printf '\tb) Find Books\n' 
printf '\tc) Edit Books\n' 
printf '\td) Remove Books\n' 
printf '\te) View Books\n' 
printf '\tf) Quit\n' 
printf 'Please enter the choice then press return\n' 
read menu_choice 
return 
} 

insert_record(){ 
echo $* >>$record_file 
return 
} 
  
  
#!!!!!!!!!...........................!!!!!!!!!!!!!!!! 
#This function ask user for details information about book for keeping records 
  
add_books(){ 
  
#prompt for information 
  
printf 'Enter Books category:-' 
read tmp 
liCatNum=${tmp%%,*} 
  
printf 'Enter Books title:-' 
read tmp 
liTitleNum=${tmp%%,*} 
  
printf 'Enter Auther Name:-' 
read tmp 
liAutherNum=${tmp%%,*} 
  
#Check that they want to enter the information 
printf 'About to add new entry\n' 
printf "$liCatNum\t$liTitleNum\t$liAutherNum\n" 
  
#If confirmed then append it to the record file 
if get_confirm; then 
   insert_record $liCatNum,$liTitleNum,$liAutherNum 
fi 
  
return 
} 

find_books(){ 
grep computer $record_file > $temp_file 
     
  set $(wc -l $temp_file) 
  linesfound=$1 
  
  case "$linesfound" in 
  0)    echo "Sorry, nothing found" 
        get_return 
        return 0 
        ;; 
  *)    echo "Found the following" 
        cat $temp_file 
        get_return 
        return 0 
  esac 
return 
} 


$temp_file 
  
 set $(wc -l $temp_file) 
   linesfound=$1 
  
   case "$linesfound" in 
   0)    echo "Sorry, nothing found\n" 
         get_return 
         return 0 
         ;; 
   *)    echo "Found the following\n" 
         cat $temp_file ;; 
        esac 
 printf "Type the books titel which you want to delete\n" 
 read searchstr 
  
  if [ "$searchstr" = "" ]; then 
      return 0 
   fi 
 grep -v "$searchstr" $record_file > $temp_file 
 mv $temp_file $record_file 
 printf "Book has been removed\n" 
 get_return 
return 
} 
  
view_books(){ 
printf "List of books are\n" 
  
cat $record_file 
get_return 
return 
} 



edit_books(){ 
  
printf "list of books are\n" 
cat $record_file 
printf "Type the tile of book you want to edit\n" 
read searchstr 
  if [ "$searchstr" = "" ]; then 
     return 0 
  fi 
  grep -v "$searchstr" $record_file > $temp_file 
  mv $temp_file $record_file 
printf "Enter the new record" 
add_books 
  
} 

rm -f $temp_file 
if [!-f $record_file];then 
touch $record_file 
fi 
  
clear 
printf '\n\n\n' 
printf 'Mini library Management' 
sleep 1 
  
quit="n" 
while [ "$quit" != "y" ]; 
do 
  
#funtion call for choice 
set_menu_choice 
case "$menu_choice" in 
a) add_books;; 
b) find_books;; 
c) edit_books;; 
d) remove_books;; 
e) view_books;; 
f) quit=y;; 
*) printf "Sorry, choice not recognized";; 
esac 
done 
# Tidy up and leave 
  
rm -f $temp_file 
echo "Finished" 
  
exit 0 

Fehler während der Ausführung:

manage.sh: line 12:  : command not found 
manage.sh: line 19: syntax error near unexpected token `)' 
manage.sh: line 19: `      y|yes|Y|Yes|YES) ' 

Und alle weiteren Vorschläge, das Skript verbessern wird geschätzt.

+3

Bitte nehmen Sie sich einen Blick: http://www.shellcheck.net/ – Cyrus

+0

Sie haben '$ temp_file' gefolgt von 'set $ (wc -l $ temp_file)'. Sie müssen die Datei erstellen, sonst gibt 'wc' einen Fehler zurück. Meintest du '> $ temp_file'? – cdarke

+0

Kudos für einen großen Chuck Code für Ihren Auftrag abgeschlossen. Es gibt zahlreiche Dinge zu kommentieren, aber ich wähle nur ein Thema ('return'). Antwort: Unix/Linux-Philosophie/std-coding-practices sagt 'return 1' (oder einen anderen Wert ungleich Null), um einen Fehlerzustand anzuzeigen. Du gibst überall '0' zurück. B. "return" als letzte Zeile einer Funktion (ohne Rückgabe eines Variablenwerts), gibt immer "0" zurück und ist redundant (nicht benötigt), da die Funktion standardmäßig "0" zurückgibt (ohne Code). Weniger Code ist fast immer besser. Viel Glück! – shellter

Antwort

0

klingt für mich wie Sie eine andere Shell als bash verwenden. Zeile 12 ist das Ende einer Funktionsdefinition zu bash, aber nicht z.B. zu csh, die }: Command not found. in ähnlicher Weise gibt, bashcase-Syntax ist bash-spezifisch, und z.B. csh Fehler in Zeile 19 mit: Too many)'s. Dies sind nicht die genauen Fehler, die Sie bekommen, also verwenden Sie nicht csh, aber Sie verwenden wahrscheinlich auch nicht bash. versuchen Sie Ihr Programm Aufruf mit:

bash manage.sh 

oder besser tun:

`which bash` 

dann das Ergebnis, dass nach einem #! in der ersten Zeile des Skripts setzen, zum Beispiel

#!/bin/bash 

oder:

#!/usr/local/bin/bash 

abhängig davon, wo sich bash auf Ihrem System befindet.

dann tun dies einmal:

chmod +x manage.sh 

und rufen Sie das Skript wie diese von da an:

./manage.sh 
0

Das Skript einige explizite Fragen hat,

  • In den folgenden Zeilen nächsten zu temp_file =/tmp/ldb. $$, um eine leere Datei zu erstellen und die erforderliche Berechtigung zu erteilen.

    touch $temp_file ; 
    chmod 644 $temp_file 
    
  • Direkt neben der Funktionsdefinition von remove_books(), versuchen Sie, die temp_file $ ausführen, die ich Ihnen raten beabsichtigen, dies nicht tun. Entfernen Sie die Zeile '$ temp_file'
  • Scheint, Sie haben den Startblock für die Funktion find_books() verpasst.
  • Sie versuchen, die Anzahl der Zeilen in ldb-Datei zu finden, indem Sie die folgenden Zeilen

    set $(wc -l $temp_file) 
    linesfound=$1 
    

    Ausführung Whcih nicht korrekt zu sein scheint.Stattdessen erreichen Sie könnten die unterhalb der Linie der Ausführung

    linesfound=`cat $temp_file|wc -l` 
    

Hier ist das modifizierte Skript mit schnellen Lösungen,

menu_choice="" 
record_file="bookRecords.ldb" 
temp_file=/tmp/ldb.$$ 
touch $temp_file; chmod 644 $temp_file 
trap 'rm -f $temp_file' EXIT 


get_return(){ 
printf '\tPress return\n' 
read x 
return 0 
} 

get_confirm(){ 
printf '\tAre you sure?\n' 
while true 
do 
    read x 
    case "$x" in 
     y|yes|Y|Yes|YES) 
     return 0;; 
     n|no|N|No|NO) 
      printf '\ncancelled\n' 
      return 1;; 
     *) printf 'Please enter yes or no';; 
    esac 
done 
} 

set_menu_choice(){ 
clear 
printf 'Options:-' 
printf '\n' 
printf '\ta) Add new Books records\n' 
printf '\tb) Find Books\n' 
printf '\tc) Edit Books\n' 
printf '\td) Remove Books\n' 
printf '\te) View Books\n' 
printf '\tf) Quit\n' 
printf 'Please enter the choice then press return\n' 
read menu_choice 
return 
} 

insert_record(){ 
echo $* >>$record_file 
return 
} 


#!!!!!!!!!...........................!!!!!!!!!!!!!!!! 
#This function ask user for details information about book for keeping records 

add_books(){ 

#prompt for information 

printf 'Enter Books category:-' 
read tmp 
liCatNum=${tmp%%,*} 

printf 'Enter Books title:-' 
read tmp 
liTitleNum=${tmp%%,*} 

printf 'Enter Auther Name:-' 
read tmp 
liAutherNum=${tmp%%,*} 

#Check that they want to enter the information 
printf 'About to add new entry\n' 
printf "$liCatNum\t$liTitleNum\t$liAutherNum\n" 

#If confirmed then append it to the record file 
if get_confirm; then 
    insert_record $liCatNum,$liTitleNum,$liAutherNum 
fi 

return 
} 

find_books(){ 
    echo "Enter book title to find:" 
    read book2find 
    grep $book2find $record_file > $temp_file 

    # set $(wc -l $temp_file) 
    # linesfound=$1 
    linesfound=`cat $temp_file|wc -l` 

    case `echo $linesfound` in 
    0) echo "Sorry, nothing found" 
     get_return 
     return 0 
     ;; 
    *) echo "Found the following" 
     cat $temp_file 
     get_return 
     return 0 
    esac 
return 
} 

remove_books() { 
# $temp_file 

    #set $(wc -l $temp_file) 
    #linesfound=$1 
    linesfound=`cat $record_file|wc -l` 

    case `echo $linesfound` in 
    0) echo "Sorry, nothing found\n" 
     get_return 
     return 0 
     ;; 
    *) echo "Found the following\n" 
     cat $record_file ;; 
     esac 
printf "Type the books titel which you want to delete\n" 
read searchstr 

    if [ "$searchstr" = "" ]; then 
     return 0 
    fi 
grep -v "$searchstr" $record_file > $temp_file 
mv $temp_file $record_file 
printf "Book has been removed\n" 
get_return 
return 
} 

view_books(){ 
printf "List of books are\n" 

cat $record_file 
get_return 
return 
} 



edit_books(){ 

printf "list of books are\n" 
cat $record_file 
printf "Type the tile of book you want to edit\n" 
read searchstr 
    if [ "$searchstr" = "" ]; then 
    return 0 
    fi 
    grep -v "$searchstr" $record_file > $temp_file 
    mv $temp_file $record_file 
printf "Enter the new record" 
add_books 

} 

rm -f $temp_file 
if [!-f $record_file];then 
touch $record_file 
fi 

clear 
printf '\n\n\n' 
printf 'Mini library Management' 
sleep 1 

quit="n" 
while [ "$quit" != "y" ]; 
do 

#funtion call for choice 
set_menu_choice 
case "$menu_choice" in 
a) add_books;; 
b) find_books;; 
c) edit_books;; 
d) remove_books;; 
e) view_books;; 
f) quit=y;; 
*) printf "Sorry, choice not recognized";; 
esac 
done 
# Tidy up and leave 

rm -f $temp_file 
echo "Finished" 

exit 0