這非常簡單,但由於我仍然是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中沒有空的函數嗎?
圖片都沒有,我們如何共享文本。 – 2012-04-10 18:50:38
對不起,有代碼插入的問題,所以現在我添加的圖像我終於發現我錯了什麼地方 – Sam007 2012-04-10 18:52:49
當你調用一個函數時不包括括號:'create' not'create()' – 2012-04-10 22:20:31