It’s easy to see how functions can help save typing in a single script, but how about if you just happen to use the same single code block between scripts? It would seem to not help if you have to define the same function in each script, only to use it one time in each script.
There’s a solution for that problem! The bash shell allows you to create a library file for your functions, then reference that single library file in as many scripts as you need to.
The first step in the process is to create a common library file that contains the functions you need in your scripts. Here’s a simple library file called myfuncs that defines three simple functions:$ cat myfuncs
The next step is to include the myfuncs library file in your script files that want to use any of the functions. This is where things get tricky.
The problem is with the scope of shell functions. Just as with environment variables, shell functions are only valid for the shell session in which you define them. If you run the myfuncs shell script from your shell command line interface prompt, the shell creates a new shell, and runs the script in that new shell. This will define the three functions for that shell, but when you try to run another script that uses those functions, they won’t be available.
This applies to scripts as well. If you try to just run the library file as a regular script file, the functions won’t appear in your script:$ cat badtest4
The key to using function libraries is the source command. The source command executes commands within the current shell context instead of creating a new shell to execute them. You use the source command to run the library file script inside of your shell script. This makes the functions available to the script.
The source command has a shortcut alias, called the dot operator. To source the myfuncs library file in a shell script, all you need to do is add the following line:
This example assumes that the myfuncs library file is located in the same directory as the shell script. If not, you’ll need to use the appropriate path to access the file. Here’s an example of creating a script that uses the myfuncs library file:$ cat test14
The script successfully uses the functions defined in the myfuncs library file.
Shell Scripting Related Interview Questions
|Perl Scripting Interview Questions||Python Interview Questions|
|Linux Interview Questions||Linux Embedded systems Interview Questions|
|AWK Interview Questions||BioPerl Interview Questions|
|Sed (Stream Editor) Interview Questions||Advanced Linux Interview Questions|
|Unix/Linux Interview Questions||Unix Shell Scripting Interview Questions|
Shell Scripting Tutorial
Starting With Linux Shells
Getting To The Shell
Basic Bash Shell Commands
More Bash Shell Commands
Using Linux Environment Variables
Basic Script Building
Understanding Linux File Permissions
Working With Editors
Using Structured Commands
More Structured Commands
Handling User Input
Adding Color To Scripts
Introducing Sed And Gawk
The Ash Shell
The Tcsh Shell
The Korn Shell
The Zsh Shell
Using The Web
Using A Database
Shell Script For Administrators
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.