这真的很简单,但由于我仍然是 shell 脚本的新手,我无法做到这一点。这很简单,我有一个菜单驱动的脚本,当用户输入他的选择时,我会在 case 函数中循环并执行选定的菜单。到目前为止,我正在用同一个脚本编写整个代码。现在我将代码放在外部脚本中的每个菜单选项中,并从那里访问代码。所以它是这样的,
#!/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() 函数
代码在链接中,
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 函数的地方,它是空函数。我们不能在 bash 中有一个空函数吗?