Presentation is loading. Please wait.

Presentation is loading. Please wait.

Agenda Functions: What are functions? / Purpose of Functions

Similar presentations


Presentation on theme: "Agenda Functions: What are functions? / Purpose of Functions"— Presentation transcript:

1 Agenda Functions: What are functions? / Purpose of Functions
Structure of a function Passing arguments to functions The getopts function

2 What are functions? Functions are considered to be “small shell scripts” which are run within shell scripts. Functions usually are small shell scripts that perform repetitive tasks. Functions reduce repetition within shell scripts. Once a function is created, it may be able to be used in other shell scripts that you create. Often, shell script functions are available on the Internet to copy and paste.

3 Structure of a function
A function is simply a name (do not use utility or variable names), followed by open and closed round brackets. Instructions to be performed when function is run (or “called”) is contained within open and closed braces. Functions can be created and run from the shell prompt, or can be used in shell scripts

4 Structure of a function
Here is an example of a function: wt() { echo “Here are people on $HOST to chat with:” echo Who –T | grep “+” } Note: to run or “call” function, simply type wt at the shell prompt. Functions must appear before main portion of shell script that runs or “calls” the function… Indent within braces to make it easier to read when contained in a shell script.

5 Passing arguments to functions
Since functions can be considered “tiny shell scripts” within shell scripts, you can actually use arguments (also known as parameters) up to the function to be used. The same concept of “positional parameters” are used when arguments are passed up to a function. When the function ends, it does not affect the positional parameters that were set by the main shell script.

6 Passing arguments to functions
Example: print_name () { echo “Your name is: $1” } print_name “Murray Saul” Function – appearing before main portion of shell script This is what happens: The function “print_name” is read and stored into memory. Then the main shell script runs the “print_name” function and passes up the argument “Murray Saul” and is stored as the first positional parameter. Then the function runs and uses the first positional parameter and displays message. The function then ends (child process dies) and returns to the main shell script (parent process). Main portion of shell script

7 The getopts function The getopts function is a useful function for error-checking your script’s options. The getopts function can be used in conjunction with the while statement to “take action” based on a valid argument letter or number. Symbols * and ? are used to generate error messages for incorrect options selected.

8 The getopts function A colon “:” after an option indicates that another argument must follow the option. while getopts abc:d: arg do case $arg in a) echo "option is -a“ ;; b) echo "option is -b“ ;; :) printf "$0: You must supply an argument to bash %OPTARG." >& ;; \?) echo "Usage: get_0 [-abc {arg} d {arg}] " >& exit ;; esac done exit 0 Take action if option is either a or b. This example left-out actions to take if option was c or d. Colon as a “case” indicates error message if argument doesn’t match valid options

9


Download ppt "Agenda Functions: What are functions? / Purpose of Functions"

Similar presentations


Ads by Google