Automation

last person joined: 8 days ago 

Ask questions and share experiences about Apstra, Paragon, and all things network automation.
  • 1.  The 'expr' SLAX shorthand

    Posted 06-03-2010 09:51

      From the JUNOS as a scripting language training course, I understand that the SLAX 'expr' keywork is a shorthand for the xsl value of tag which outputs the value of an XPATH expression.  However, its behavior is still somewhat mysterious to me.  In more than one place in the training course, code snipets are shown that would lead someone to believe that simply using a line like this:

     

    expr "Hello World!";

     

    would actually output something...which it does not.   What I've noticed is that even:

     

    <output> expr "Hello World!";

     

    does not work, you need:

     

    <output> {

        expr "Hello World!";

    }

     

    Why is that?  Also it seems that expr is used when a lot of the Junos extention functions are called, such as:

     

    expr jcs:output("message");

    or

    expr jcs:close($conn);

     

    I don't quite understand the use of expr here....what does it do?

     

     



  • 2.  RE: The 'expr' SLAX shorthand
    Best Answer

    Posted 06-03-2010 17:32

    This is a quirk of the SLAX to XSLT syntax. The easiest way to look at it is to say that expr writes text to the result tree, so if you do this:

     

    expr "Hello World!";

     

    Then that text is written to the result tree, but keep in mind that writing to the result tree does not equal writing to the console. Writing to the console is achieved through special functions like jcs:output() or result tree elements like <output>.

     

    The reason why this works is because you are assigning that text to the <output> element, which Junos uses to write text to the result tree:

     

    <output> {

        expr "Hello World";

    }

     

    This doesn't work because it is invalid SLAX syntax: <output> expr "Hello Word": expr is only valid as the start of a line.

     

    Typically you would use the expr "string" notation when you want to assign a value to a variable conditionally. This works because you are actually redirection the result tree to a variable, which becomes a result tree fragment data type.

     

    So:

     

    var $result  = {

        if( $A == $B) {

            expr "1";

        }

        else {

            expr "2";

        }

    }

     

    Is actually writing either 1 or 2 to the result tree, but then redirecting this into the variable $result. $result technically has a data type of result tree fragment rather than string, but you can treat it as a string within your script without any problem because SLAX will automatically convert between data types.

     

    The last topic to mention is that of functions. expr is used in front of function calls simply because it is a requirement of the SLAX syntax. This stems from the translation process from SLAX to XSLT. Basically, without the expr statement it would be difficult for the SLAX converter to detect a leading function call. So, the rule is that any standalone function has to be started by expr:

     

    expr jcs:syslog(...)

    expr jcs:output(...)

     

    etc...

     

    Note, that if the function returns any value then that value will be output to the result tree by the expr function. The way I like to think about it is that SLAX has to try to do something with the function return value, even if the function will never return any value. So, if you aren't assigning the function value to a variable, using it as a function argument, or within a location path, then you have to try to write it to the result tree through the "expr" statement.



  • 3.  RE: The 'expr' SLAX shorthand

    Posted 06-07-2010 07:36

    Great info, thanks for the reply.