Convert script to create in terminal

Does anyone have a solution to take a script and convert it so it can be be added in a terminal window?

For example, if I have the following script named “script1”:

# Get NTP status
# ----------------------------------
:local ntpstatus ""
:if ([:len [/system package find where !disabled and name=ntp]] > 0 or [:tonum [:pick [/system resource get version] 0 1]] > 6) do={
    :set ntpstatus [/system ntp client get status]
} else={
    :if ([:typeof [/system ntp client get last-update-from]] = "nil") do={
        :set ntpstatus "using-local-clock"
    } else={
        :set ntpstatus "synchronized"
    }
}
:log info message="script=ntp status=$ntpstatus"

I would like to be able to convert it to the following so that the /system/script/add command can be inside another script:

/system script add dont-require-permissions=no name=WoL-Activation owner=admin policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive source="# Get NTP status]\r\
\n # ----------------------------------\r\
\n:local ntpstatus ;\r\
\n:if ([:len [/system package find where !disabled and name=ntp]] > 0 or [:tonum [:pick [/system resource get version] 0 1]] > 6) do={\r\
\n    :set ntpstatus [/system ntp client get status]\r\
\n} else={\r\
\n    :if ([:typeof [/system ntp client get last-update-from]] = "nil") do={\r\
\n       :set ntpstatus "using-local-clock"\r\
\n    } else={\r\
\n        :set ntpstatus "synchronized"\r\
\n    }\r\
\n}\r\
\n:log info message="script=ntp status=$ntpstatus" \r\

A different (but related to the main project) question: the code immediately above (with the /r/ and /n added) fails with the quotation marks (") after “ntpstatus” And, if I remove those quotation marks, it then fails at “nil” – is there an solution to this?

Thank you.

I think I solved my problem.

In order to use single (or double) quotation marks we need an “escape” character.

The following, for example, works:

\n:local ntpstatus \"\"\r\

And, the following code entered in a terminal creates the script. One trick is the use of \n and \r\ on each line. Another trick is to add source=“” (note the close quote mark).

/system script add name=test3 source="##############Script Settings##################\r\
    \n\r\
\n# Get NTP status\r\
\n# ----------------------------------\r\
\n:local ntpstatus \"\"\r\
\n:if ([:len [/system package find where !disabled and name=ntp]] > 0 or [:tonum [:pick [/system resource get version] 0 1]] > 6) do={\r\
\n    :set ntpstatus [/system ntp client get status]\r\
\n} else={\r\
\n    :if ([:typeof [/system ntp client get last-update-from]] = \"nil\") do={\r\
\n        :set ntpstatus \"using-local-clock\"\r\
\n    } else={\r\
\n        :set ntpstatus \"synchronized\"\r\
\n    }\r\
\n}\r\
\n:log info message=\"script=ntp status=$ntpstatus\"\r\
\n\r\
\n"

The escaping does get tricky.

You can also use “/system/script export where name=XXXX” to get the “escaped form” of any script (and then cut-and-paste that as needed).

Good to know, thanks :slight_smile: .

That is super useful!