Previous Section  < Day Day Up >  Next Section

LAB 47: AUTOLOADING FUNCTIONS

Steps for autoloading a function:

1. Make a directory called myfunctions.

2. Change directory to myfunctions and use the editor to create a file called good-bye.

3. Insert in the good-bye file a function called good-bye, spelled exactly the same as the filename.

4. The good-bye function contains:


function good-bye {

print The current time is $(date)

print "The name of this script is $0"

print See you later $1

print Your machine is `uname –n`

}


5. Write and quit the editor. You now have a file containing a function with the same name.

6. Go to your home directory. Modify the .kshrc file in the editor by typing the following line:


FPATH=$HOME/myfunctions


7. Exit the editor, and to execute the .kshrc in the current environment, use the dot command.

8. In the timegreet script you wrote in Lab 45, include the following lines:


autoload good-bye

good-bye $LOGNAME


9. Run the timegreet script. The good-bye function output will appear.

10. Create functions for each of the menu items in the lookup script. Store the functions in a file called lookup_functions in a directory called myfunctions.

11. Autoload the functions in your lookup script and make the function calls for the corresponding cases.

12. Use the trap command so that if the user enters a menu selection other than an integer value, the trap command will print an error to the screen, and cause the script to ask the user to re-enter the correct data type.

    Previous Section  < Day Day Up >  Next Section