[ Pobierz całość w formacie PDF ]

Functions
Functions are coded procedures within the script engine. A script can
invoke functions, which take arguments, perform a specific action, and
return the result. This process might sound similar to a named template,
but there are large differences between the two:
n A named template is actual script code, whereas a function is part of
the underlying Junos operating system itself.
n Values are provided to named templates through the use of
parameters, which are assigned by name, but functions use arguments
where a precise order is mandated.
n Functions actually return results, whereas named templates can only
write to the result tree and have that result tree fragment redirected to a
variable in the calling function.
50 Day One: Applying Junos Operations Automation
The syntax of functions differs from that of named templates as well.
The call statement is not used; only the function name is provided and
the required arguments specified within parenthesis:
expr substring( $localtime, 1, 3 );
Functions return values, as an example the substring() function returns a
string. The above code writes the string value to the result tree. The
script code could assign the string value to a variable instead using the
following syntax:
var $day-string = substring( $localtime, 1, 3 );
Note the difference between assigning a value to a variable from a
named template versus from a function:
var $day-of-week = { call get-day-of-week(); }
String Functions
String functions are used regularly within scripts. Table 3.2 lists some of
the most common and useful of the string functions.
Table 3.2 String Functions
Example
Function
Explanation
var $hello-string = substring(  Hello World , 1, 5 );
Takes a starting string and returns the substring that begins at
substring( string-value,
the specified index and extends for the given length. This
starting-index, length )
example results in the $hello-string being assigned the string
value  Hello . In SLAX, indexes always begin with 1.
var $hello-string = substring-before(  Hello World ,  
);
Returns a substring of string-value-1, but in this case the size
substring-before( string-
of the substring is determined by the location of string-value-2
value-1, string-value-2 )
within string-value-1. The function returns the entire portion
of string-value-1 up to string-value-2. The example results
in $hello-string being assigned the string value  Hello .
var $world-string = substring-after(  Hello World ,   );
Returns the portion of string-value-1 which comes after
substring-after( string-
string-value-2 [i.e.,the reverse logic of substring-be-
value-1, string-value-2 )
fore()]. This example sets $world-string to the string value
 World .
Chapter 3: Understanding SLAX Language Fundamentals 51
if( contains( $interface-name, "ge-" ) ) {
"The interface is Gigabit-Ethernet";
}
Returns a boolean value of true or false. If string-value-1
contains( string-value-1,
contains string-value-2 then it returns true, otherwise it
string-value-2 )
returns false. The example shows the contains() function. This
code uses it to determine if an interface is a ge or not based on
the presence of the second string  ge- in the string $interface-
name. If the returned value is true then a string is written to the
result tree.
if( starts-with( $interface-name, "ge-" ) ) {
"The interface is Gigabit-Ethernet";
}
starts-with( string-
value-1, string-value-2 )
Returns a boolean value of true if string-value-1 begins with
string-value-2, otherwise it returns false. In the example, if
$interface-name begins with  ge- then a string is written to
the result tree.
expr string-length("ospf");
string-length( string-
Returns the number of characters within the string. The exam-
value )
ple causes the value 4 to be written to the result tree.
var $new-string = translate( $string, "abcdefghijklmnopqr
stuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
The translate() function translates the characters within the
translate( string-value,
string-value, where the function translates any matching
from-string, to-string )
characters within from-string to their corresponding characters
in to-string and returns the result. This example translates a
string into upper-case.
MORE? This booklet only covers a portion of the available functions. Additional
functions are discussed in the Configuration and Diagnostic Automation
Guide at www.juniper.net/techpubs.
jcs:printf()
Printing unformatted output to the screen is sufficient for some scripts,
but in many cases it is more user-friendly to have formatted script output
where each line follows the same column spacing. The jcs:printf()
function is used in op scripts for this purpose. It returns a string based on
the formatting instructions and values provided in the arguments.
NOTE The jcs:printf() function does not output directly to the console, it
only returns a formatted string. This string can then be output to the
console as desired.
52 Day One: Applying Junos Operations Automation
The syntax for jcs:printf() is the following:
jcs:printf( "expression", value1, value2, ..., valuex );
The string expression contains embedded placeholders that indicate
where each value should be inserted, as well as the format in which
they should be placed. Here is an example:
"123456789012345678901234567890";
jcs:printf("%-10s%-10s","OSPF","ISIS" );
jcs:printf("%10s%10s", "OSPF", "ISIS" );
There are two embedded placeholders in the expression of each of [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • actus.htw.pl