29.04.2019 Views

OS6860(E)_AOS_8.1.1.R01_Switch_Management_Guide

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Web Services, CLI Scripting, and OpenFlow<br />

CLI Scripting<br />

Variables and functions<br />

Variables<br />

The asterisk character ('*') and the question mark have very specific meanings in Bash. The asterisk character<br />

can be used to replace an arbitrary number of characters of a command with a file name. This file<br />

needs to be referenced in a way that lets Bash find it. For instance, the following will list all the files found<br />

in the current directory that begin with the letter 'a' and end with the letter 'c'.<br />

-> ls a*c<br />

Similarly, the question mark will be replaced by a single character. Therefore, the following will list all<br />

files, in the current directory, that are three characters long, begin with the letter 'a' and end with the letter<br />

'c'. Three characters long because '?' can only be replaced by a single character.<br />

-> ls a?c<br />

The dollar sign prefix is used to name variables.Assigning a value to a variable is done without the dollar<br />

sign prefix as shown below.<br />

-> A="hello there"<br />

-> echo $A<br />

hello there<br />

Variables can be used in CLI commands. For instance:<br />

Functions<br />

-> MYIF=192.168.1.1<br />

-> ip interface $MYIF<br />

-> show ip interface $MYIF<br />

A function is a piece of code that can be reused after creating it. It can take parameters and return a diagnostic<br />

value. As a simple example is there’s a need to repetively create VLANs with similar parameters a<br />

function can be used to avoid having to specify these parameters every time.<br />

To create a function, type its name followed by a pair of parenthesis and an opening curly brace. To<br />

complete the function definition, enter a closing curly brace. The body of the function will go between<br />

both curly braces, the function can then be run by entering its name as in the example below:<br />

function myvlans()<br />

{<br />

}<br />

To handle parameters within the function, positional parameters are used. For instance the following will<br />

create VLAN 5:<br />

function myvlans()<br />

{<br />

vlan $1<br />

}<br />

-> myvlans 5<br />

Additional functionality can be added. As an example the function can be enhanced to handle cases when<br />

the user forgets to pass a parameter.<br />

Omni<strong>Switch</strong> <strong>AOS</strong> Release 8 <strong>Switch</strong> <strong>Management</strong> <strong>Guide</strong> May 2014 page 10-21

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!