User guide

CLI Scripting Web Services, CLI Scripting and OpenFlow
page 11-22 OmniSwitch AOS Release 7 Switch Management Guide March 2015
function myvlans()
{
if [ $# -lt 1 ]; then
echo "Please provide a paramater"
else
vlan $1
fi
}
-> myvlans
This will display an error message because $#, which represents the number of arguments that were passed
to the function, is less than ("-lt") one.
Shift can be used to cycle through a parameter list so that multiple parameters can be used with a function.
The example below creates each VLAN using the "vlan" command. Every parameter will end up being
seen as "parameter 1" due to the "shift" command. Shift moves all the positional parameters down by one
every time it is invoked as in the example below:
function myvlans()
{
while [ "$1" != "" ]; do
vlan $1
shift
done
}
-> myvlans 5 6 7
Now, the script will “shift” the parameters, cycling through them:
$1="5", $2="6", $3="7"
> shift
$1="6", $2="7"
> shift
$1="7"
Additional functionality can be added to check that a VLAN was successfuly created before moving on to
the next one. This can be done using the previous command's return code which is stored in $?, for
instance:
function myvlans()
{
while [ "$1" != "" ]; do
vlan $1
if [ $? -ne 0 ]; do
echo "Error!"
return 1
done
shift
done
}
-> myvlans 5 6 7
If "vlan $1" returned a value other than "0" which traditionally denotes success, the script returns immedi-
ately.