2012-04-10 195 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