User guide
Web Services, CLI Scripting and OpenFlow CLI Scripting
OmniSwitch AOS Release 7 Switch Management Guide March 2015 page 11-21
Variables and functions
Variables
The asterisk character ('*') and the question mark have very specific meanings in Bash. The asterisk char-
acter can be used to replace an arbitrary number of characters of a command with a file name. This file
needs to be referenced in a way that lets Bash find it. For instance, the following will list all the files found
in the current directory that begin with the letter 'a' and end with the letter 'c'.
-> ls a*c
Similarly, the question mark will be replaced by a single character. Therefore, the following will list all
files, in the current directory, that are three characters long, begin with the letter 'a' and end with the letter
'c'. Three characters long because '?' can only be replaced by a single character.
-> ls a?c
The dollar sign prefix is used to name variables.Assigning a value to a variable is done without the dollar
sign prefix as shown below.
-> A="hello there"
-> echo $A
hello there
Variables can be used in CLI commands. For instance:
-> MYIF=192.168.1.1
-> ip interface $MYIF
-> show ip interface $MYIF
Functions
A function is a piece of code that can be reused after creating it. It can take parameters and return a diag-
nostic value. As a simple example is there’s a need to repetively create VLANs with similar parameters a
function can be used to avoid having to specify these parameters every time.
To create a function, type its name followed by a pair of parenthesis and an opening curly brace. To
complete the function definition, enter a closing curly brace. The body of the function will go between
both curly braces, the function can then be run by entering its name as in the example below:
function myvlans()
{
}
To handle parameters within the function, positional parameters are used. For instance the following will
create VLAN 5:
function myvlans()
{
vlan $1
}
-> myvlans 5
Additional functionality can be added. As an example the function can be enhanced to handle cases when
the user forgets to pass a parameter.