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.

CLI Scripting<br />

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

function myvlans()<br />

{<br />

if [ $# -lt 1 ]; then<br />

echo "Please provide a paramater"<br />

else<br />

vlan $1<br />

fi<br />

}<br />

-> myvlans<br />

This will display an error message because $#, which represents the number of arguments that were passed<br />

to the function, is less than ("-lt") one.<br />

Shift can be used to cycle through a parameter list so that multiple parameters can be used with a function.<br />

The example below creates each VLAN using the "vlan" command. Every parameter will end up being<br />

seen as "parameter 1" due to the "shift" command. Shift moves all the positional parameters down by one<br />

every time it is invoked as in the example below:<br />

function myvlans()<br />

{<br />

while [ "$1" != "" ]; do<br />

vlan $1<br />

shift<br />

done<br />

}<br />

-> myvlans 5 6 7<br />

Now, the script will “shift” the parameters, cycling through them:<br />

$1="5", $2="6", $3="7"<br />

> shift<br />

$1="6", $2="7"<br />

> shift<br />

$1="7"<br />

Additional functionality can be added to check that a VLAN was successfuly created before moving on to<br />

the next one. This can be done using the previous command's return code which is stored in $?, for<br />

instance:<br />

function myvlans()<br />

{<br />

while [ "$1" != "" ]; do<br />

vlan $1<br />

if [ $? -ne 0 ]; do<br />

echo "Error!"<br />

return 1<br />

done<br />

shift<br />

done<br />

}<br />

-> myvlans 5 6 7<br />

If "vlan $1" returned a value other than "0" which traditionally denotes success, the script returns immediately.<br />

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

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

Saved successfully!

Ooh no, something went wrong!