Blogs

Scripting How-To: Format script output options

By Erdem posted 08-09-2015 04:35

  

Script Format Options

 

For SLAX version 1.0 and higher, you can format script output options.

 

Resolution

 

First, if you want spaced columns then you will want to use the
jcs:printf() function. This returns a correctly spaced string
which can then be displayed on the console through the
jcs:output() function. Here is an example from eol-scanner.slax
that uses jcs:printf():

 

NOTE: The following script example is obsolete and repeated as an example only.

https://github.com/Juniper/junoscriptorium/blob/master/library/juniper/op/display/eol-scanner/eol-scanner.slax

 

1	/* Display the dates:
2	12345678901234567890123456789012345678901234567890<wbr/>1234567890123456789012345678901234567890
3	        Last Order Date: xx/xx/xxxx  Last Warranty Conversion: xx/xx/xxxx
4	  Same-day Support Ends: xx/xx/xxxx     Next-Day Support Ends: xx/xx/xxxx
5	End New Release Support: xx/xx/xxxx            End of Support: xx/xx/xxxx
6	*/

 

1	var $line1 = jcs:printf( "%24s %-10s %26s %-10s", "Last Order Date:", date[type == $last-order-date]/date, "Last Warranty Conversion:", date[type == $last-date-to-convert-warranty]/date );
2	 
3	expr jcs:output( $line1 );
4	var $line2 = jcs:printf( "%24s %-10s %26s %-10s", "Same-day Support Ends:", date[type == $same-day-support-discontinued]/date, "Next-Day Support Ends:", date[type == $next-day-support-discontinued]/date );
5	 
6	expr jcs:output( $line2 ); var $line3 = jcs:printf( "%24s %-10s %26s %-10s", "End New Release Support:", date[type == $end-of-new-junos-release-support]/date, "End of Support:", date[type == $end-of-support]/date );
7	 
8	expr jcs:output( $line3 );

 

The next output trick is how to combine multiple XML node values into a single string. You can do this by enclosing a for-each loop into a variable assignment and using the expr statement to write the node values into the string. Here is an example of how this can be done:

 

1	var $string = {
2	    for-each( $processes/process ) {
3	        expr pid;
4	        if( last() != position() ) {
5	            expr ",";
6	        }
7	    }
8	}

The result of this code is that the $string variable consists of a single string of <pid> node values with a comma separating each one.

 


#ScriptingHow-To
#How-To
#Slax
#outputoptions
#xml
#display