accesing functions from external files bash

3.2k views Asked by At

This is really simple but since i m still a newbiw in shell scripting, i not able to do this. It is simple, i have a menu driven script in which when the user enters his choice i loop in the case function and execute the selected menu. Uptil now i was writing the entire code in the same script. Now i am putting the code in each menu option in a external script and accessing the code from there. So it is something like this,

#!/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

Then i created another external script where I wrote the create() function

Code is in the link,

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()
{

}

But i am getting an error,

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'

Line 31 is where I am ending the addRecord function, which is empty function, for the remaining cases. Can we not have a empty function in bash?

1

There are 1 answers

3
johnsyweb On BEST ANSWER

Functions may not be empty!

--Advanced Bash-Scripting Guide: Chapter 24. Functions

Instead, try:

addRecord()
{
    return
}