[j-nsp] How to catch invalid value/option for a command in SLAX script?

Martin T m4rtntns at gmail.com
Mon Jul 11 09:23:37 EDT 2016


Thanks for the reply! Did I understand you correctly that "if(
$variable == "usb0\ninvalid value" ) {" is actually "if( string(
$variable ) == "usb0\ninvalid value" ) {" and the string() inserts a
newline at the beginning and in the end of the string? Based on the
debugger output it looks like so:

(sdb) p string( $variable )
[string] "
usb0
invalid value
"
(sdb) p string( $variable ) == "usb0\ninvalid value"
[boolean] false
(sdb) p string( $variable ) == "\nusb0\ninvalid value\n"
[boolean] true
(sdb)


thanks,
Martin


On Sat, Jul 9, 2016 at 8:27 PM, Phil Shafer <phil at juniper.net> wrote:
> Martin T writes:
>>I have a following simple SLAX script which executes "show system
>>snapshot media usb0" command in an environment where "usb0" option for
>>"show system snapshot" command is unavailable:
>
> I can't reproduce this on my test box ("command is not valid on the m7i"),
> but the best way to investigate this would be to use the debugger,
> stop on the "==" line, and print the contents of $variable.
>
> Reached breakpoint 2, at /tmp/foo.slax:21
> foo.slax:21:     if( $variable == "usb0\ninvalid value" ) {
> (sdb) p $variable
> [node-set] (1)
> <xnm:error xmlns="http://xml.juniper.net/xnm/1.1/xnm" xmlns:xnm="http://xml.juniper.net/xnm/1.1/xnm">
> <message>
> command is not valid on the m7i
> </message>
> </xnm:error>
>
> My guess is that the implicit string() call in the equals test is
> getting you.
>
> Consider using contain() instead of equals::
>
> (sdb) p string($variable) == "\n\ncommand is not valid on m7i\n\n"
> [boolean] false
>
> Thanks,
>  Phil


More information about the juniper-nsp mailing list