Shell Functions  «Prev  Next»
Lesson 8Obtaining functions from others
ObjectiveDetermine how and when to use functions that you did not write.

Borrowing Shell Script Functions

You can obtain shell scripts "resources to find script examples" from textbooks, fellow programmers, the Internet, and anywhere else you can find them.

Resources to find script examples

Most textbooks that cover shell scripting contain useful scripts. The following texts are recommended:
  1. UNIX Shells by Example Ellie Quigley Prentice Hall 1997 ISBN 0-13-460866-6 UNIX Shells by Example
  2. Beginning Portable Shell Scripting Portable Shell Scripting

Best technique for borrowing a Shell Script Functions from another Source

When borrowing a shell script function from another source, it's important to use best practices to ensure that the function is safe and reliable. Here are some techniques for borrowing shell script functions from another source:
  1. Review the source: Before borrowing a function from another source, review the source code to ensure that it is trustworthy and reliable. Make sure that the function is well-documented and that the source is reputable.
  2. Use version control: If the function is part of a larger script, use version control tools like Git or SVN to track changes and updates to the script. This will allow you to easily roll back changes if something goes wrong or if you need to revert to a previous version of the script.
  3. Use namespaces: Use namespaces to avoid conflicts with existing functions or variables in your own script. For example, if you're borrowing a function called foo, consider renaming it to other_foo to avoid conflicts with any existing functions called foo in your own script.
  4. Test the function: Before using the function in your own script, test it thoroughly to ensure that it works as expected and that it doesn't introduce any new issues or errors.
  5. Credit the original author: If you're using a function that was created by someone else, credit the original author in your own script by including a comment with their name or a link to the original source.
  6. Adapt the function to your needs: If the function you're borrowing doesn't quite meet your needs, consider adapting it to fit your specific use case. This might involve modifying the function to accept different inputs or to produce different outputs.

By following these techniques, you can ensure that the function you're borrowing is safe, reliable, and fits your specific needs.

Inherit Set of Shell Scripts

Often you will inherit a set of shell scripts from the former holder of your position at work. Using code written by others can save you many hours of programming time, but you should use these scripts with caution. If you do use a script written by someone else, you should check the following things:
  1. Check the #! line at the top of the script. It should contain just the name of the shell that the script was written for.
  2. Do not run scripts from other users when you are logged in as root unless you understand every line of code in the script. Root has all-powerful permission on the machine and it is toodangerous.
  3. Read through the script and make sure you understand what it does.
  4. Even if the script is from someone you trust, that person may have copied the code from someone else. Find out who the author of the script is.
The next lesson wraps up the module.