从外部文件bash访问函数

时间:2012-04-10 18:49:12

标签: bash bash-function

这很简单,但由于我仍然是shell脚本中的新手,我无法做到这一点。这很简单,我有一个菜单驱动的脚本,当用户输入他的选择时,我在case函数中循环并执行所选菜单。 Uptil现在我在同一个脚本中编写完整的代码。现在我将每个菜单选项中的代码放在外部脚本中并从那里访问代码。所以就是这样,

#!/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中没有空函数吗?

1 个答案:

答案 0 :(得分:2)

  

功能可能不是空的!

- Advanced Bash-Scripting Guide: Chapter 24. Functions

相反,请尝试:

addRecord()
{
    return
}