Page 1 of 1

Can someone explain me this small script

Posted: Sat Jun 01, 2013 12:50 am
by pkisistemas2
:local date [/system clock get date]
:local months ("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec")
:local month ([:find $months [:pick $date 0 3 ]] + 1)
:if ($month < 10) do={:set month ("0" . $month);}
:local name "$[/system identity get name].$[:pick $date 7 11].$month.$[:pick $date 4 6].backup"
/system backup save name=$name
/tool fetch address=192.168.92.10 mode=ftp upload=yes user=jeff password=test port=14147 src-path=$name dst-path=$name


---------------------------------------------

I understand what it do... but not how it do, exactly, i dont understand this line, and how extract value for variable "month"

:local month ([:find $months [:pick $date 0 3 ]] + 1)

Re: Can someone explain me this small script

Posted: Sat Jun 01, 2013 1:56 am
by boen_robot
Well, let's break that line down:
:local month ([:find $months [:pick $date 0 3 ]] + 1)
1.
:local month (...)
Creates a variable called "month" with a value determined in the brackets.

2.
[...] + 1
A value is being formed from whatever the command between "[" and "]" returns, plus 1.

3.
:find $months [...]
The ":find" command, when applied to an array (and in this case, $months is an array) will return the index of the element that has a certain value. In this case, the value is specified as being whatever the command between "[" and "]" returns. Here, it's important to note that arrays are comprised of multiple values, with each being accessible by an index.

Note that in
:local months ("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec")
"jan" is at index 0, "feb" is at index 1, and so on up to "dec" which is at index 11.

4.
:pick $date 0 3
When applied to a string (and $date is a string), it gets a portion of the string, starting at the position specified in the second argument (0 in this case), and from that point on gets the number of characters specified in the third argument (3 in this case).

Because of how the date is formatted, getting the first 3 characters is always going to be one of "jan", "feb", etc.

Let's backtrack from that...

5.
:find $months [:pick $date 0 3]
This will always return a number between 0 and 11, since the first 3 characters are always a month name, and the $months array contains nothing but month names.

6.
:local month ([:find $months [:pick $date 0 3 ]] + 1)
Because of the way $months is structured, having 1 added to a number between 0 and 11 is always going to essentially convert the month name to the month's number.

Re: Can someone explain me this small script

Posted: Sat Jun 01, 2013 11:40 am
by pkisistemas2
Many thanks... teacher, :D :D :D

Re: Can someone explain me this small script

Posted: Sat Jun 01, 2013 12:22 pm
by nishadul
Many thanks... teacher, :D :D :D