嘿我试图使用Shell脚本模拟一个简单的图书馆管理系统,作为我的大学UNIX作业的一部分,但我在剧本中遇到了一些奇怪的错误。
这是我的剧本
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
执行时出现错误:
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) '
任何进一步改进脚本的建议都将受到赞赏。
答案 0 :(得分:0)
听起来像你在使用bash
之外的其他一些shell。第12行是bash
的函数定义的结尾,但不是例如至csh
}: Command not found.
类似bash
' case
语法bash
具体,{第19行上出现csh
次错误:Too many )'s.
这些错误并不是您获得的确切错误,因此您未使用csh
,但是您已经#39} ;可能也没有使用bash
。尝试使用以下方式调用您的程序:
bash manage.sh
或者更好的是:
`which bash`
然后在#!
之后将结果放在脚本的第一行,例如,
#!/bin/bash
或:
#!/usr/local/bin/bash
取决于系统中bash
的位置。
然后这样做一次:
chmod +x manage.sh
并从此调用您的脚本:
./manage.sh
答案 1 :(得分:0)
该脚本有一些明确的问题,
在temp_file = / tmp / ldb。$$旁边添加以下行以创建一个空文件并授予必要的权限。
touch $temp_file ;
chmod 644 $temp_file
您正试图通过执行以下行
来查找ldb文件中的行数set $(wc -l $temp_file)
linesfound=$1
whcih似乎不正确。相反,你可以实现以下行,
linesfound=`cat $temp_file|wc -l`
以下是带有快速修复的修改过的脚本
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