Agenda Functions What are functions Purpose of Functions

  • Slides: 9
Download presentation
Agenda Functions: What are functions? / Purpose of Functions Structure of a function Passing

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

What are functions? Functions are considered to be “small shell scripts” which are run

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.

Structure of a function A function is simply a name (do not use utility

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

Structure of a function Here is an example of a function: wt() { Functions

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

Passing arguments to functions Since functions can be considered “tiny shell scripts” within shell

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.

Passing arguments to functions Example: Function – appearing before main portion of shell script

Passing arguments to functions Example: Function – appearing before main portion of shell script print_name () { echo “Your name is: $1” } This is what happens: print_name “Murray Saul” Main portion of shell script 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).

The getopts function The getopts function is a useful function for error-checking your script’s

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.

The getopts function A colon “: ” after an option indicates that while getopts

The getopts function A colon “: ” after an option indicates that while getopts ab: arg another argument must follow the option. do case $arg in a) echo "option is -a“ ; ; Notice that $OPTARG notes the b) echo "option is -b“ value following that option “b”. . . echo “option value is $OPTARG” ; ; : ) printf "$0: You must supply an argument to bash %OPTARG. " >&2 ; ; ? ) echo "Usage: get_0 [-ab {arg}] " >&2 exit 1 Colon as a “case” indicates error ; ; message if argument doesn’t match esac valid options done exit 0