Question
· Jul 6, 2018

Multi-line terminal commands

Let's say I have this simple script  file try.script

write 1
write 0

I can execute it in a terminal (csession) by calling:

csession cache < try.script

And I get the following output:

%SYS>
1
%SYS>
0

However I want to use a try catch block in my script:

try {
write 1/0
catch {}
halt

But as script is executed line by line, it's interpreted like this:

%SYS>
TRY {
      ^
<SYNTAX>
%SYS>

WRITE 1/0
^
<DIVIDE>
%SYS>

CATCH {}
^
<SYNTAX>
%SYS>

I know that I can write it all in 1 line, but I'd rather not do that. Is there a way to feed multiline statement into Cache?

Discussion (8)1
Log in or sign up to continue

Make your script create an INT routine and run it. Example try.script below. Your lines of code either begin with the TAB character or with a label followed by TAB.

zr
    ; Test routine created by try.script file
    w !,"This is written by INT routine ",$t(+0)
    w !," which is created from a script injected to csession."
zs MyBootRoutine
d ^MyBootRoutine
; Clean up
zr  zs MyBootRoutine
h

It worked for me (2017.2.2 on Windows):

C:\InterSystems\Ens172\bin>CSESSION ENS172 < c:\s\try.script

Node: TIN, Instance: ENS172

USER>zr

USER> ; Test routine created by try.script file

USER> w !,"This is written by INT routine ",$t(+0)

USER> w !," which is created from a script injected to csession."

USER>zs MyBootRoutine

USER>d ^MyBootRoutine

This is written by INT routine MyBootRoutine
 which is created from a script injected to csession.
USER>; Clean up

USER>zr  zs MyBootRoutine

USER>h

C:\InterSystems\Ens172\bin>

Maybe your indented script lines (the ones to insert into the routine) aren't starting with $C(9)