2012-04-10 196 views
1

这非常简单,但由于我仍然是shell脚本中的新生代,我无法做到这一点。很简单,我有一个菜单驱动的脚本,当用户输入他的选择时,我循环使用case函数并执行选定的菜单。 Uptil现在我正在用相同的脚本编写完整的代码。现在我将代码放在外部脚本的每个菜单选项中,并从那里访问代码。因此,它是这样的,从外部文件访问功能bash

#!/bin/bash 
. course_func 
file=names.dat 
[ ! -f $file ] && > $file 
while true 
do 
clear 
echo ------------------------------------------ 
echo 1. Create a record 
echo 2. View Records 
echo 3. Search for records 
echo 4. Delete records that match a pattern 
echo ------------------------------------------ 
echo "Please enter your choice or enter (q) to quit: \c ";read choice 
case $choice in 
    1) 
    create() 
    echo Enter to continue; read junk 
    ;; 
     *) 
    clear 
    echo Wat nonsense enter properly 
    echo Enter to continue: ;read crap 
    ;; 
esac 
done 

然后,我创建,我写的创建()函数

代码是在链路另一外部脚本,

create() 
{ 
    while true 
    do 
     echo Please enter the name: ;read name 
     echo Please enter your surname: ;read surname 
     echo Please enter the address: ;read add 
     echo Please enter the cell no.: ;read cell 
     if yesno Do you really wish to save the following data to file 
     then 
     echo $name:$surname:$add:$cell >> $file 
     clear 
     echo The record has been inserted :D 
     else 
     echo The record was not save 
     fi 
     if yesno Do you wish to enter another record 
     then 
     : 
     else 
     xit 
     fi 
    done 
} 

addRecord() 
{ 

} 

search() 
{ 

} 

delRecord() 
{ 

} 

,但我得到一个错误,

course_func: line 31: syntax error near unexpected token `}' 
course_func: line 31: `}' 
menu_driven: line 18: syntax error near unexpected token `echo' 
menu_driven: line 18: ` echo Enter to continue; read junk' 

31行是我在哪里结束addRecord函数,它是空函数,f或其余案件。我们可以在bash中没有空的函数吗?

+1

图片都没有,我们如何共享文本。 – 2012-04-10 18:50:38

+0

对不起,有代码插入的问题,所以现在我添加的图像我终于发现我错了什么地方 – Sam007 2012-04-10 18:52:49

+0

当你调用一个函数时不包括括号:'create' not'create()' – 2012-04-10 22:20:31

回答

2

函数不能为空!

- Advanced Bash-Scripting Guide: Chapter 24. Functions

相反,尝试:

addRecord() 
{ 
    return 
} 
+0

是的,解决了前两个错误 course_func:第31行:意外标记附近的语法错误}} course_func:第31行:'}' 其他两个错误仍然存​​在。我更新了menu_driven代码 – Sam007 2012-04-10 19:09:20

+1

是的。只需要删除创建时的打开和关闭选项。现在它工作正常。谢谢@Johnsyweb – Sam007 2012-04-10 19:15:34