Question
· May 30, 2017

How to execute multi-parameter routines from single terminal command?

I need to execute a single csession command which calls a multi-parameter routine, such as ^SECURITY. This will be to do things such as create databases, resources, etc. (see below). When I run the command all at once, I get a PARAMETER error.

[user@host ~]$ csession INSTANCENAME -U %SYS "^SECURITY(4,1,\"testresource\")"
<PARAMETER>SECURITY^SECURITY

 

%SYS 2d0>do ^SECURITY

1) User setup
2) Role setup
3) Service setup
4) Resource setup
5) Application setup
6) Auditing setup
7) Domain setup
8) SSL configuration setup
9) Mobile phone service provider setup
10) OpenAM Identity Services setup
11) Encryption key setup
12) System parameter setup
13) X509 User setup
14) Exit
Option? 4
1) Create resource
2) Edit resource
3) List resource
4) Delete resource
5) Export resources
6) Import resources
7) Exit
Option? 1
Resource to create? testresource
Description?
Do you want resource testresource to be public? No => No
Confirm creation of resource testresource? Yes =>

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

Hi!

If you are not using OS single sign-on, this shell script should do it:

#!/bin/bash

csession AUPOLDEVENS <<EOFF
SuperUser
superuserpassword
ZN "%SYS"
Do ^SECURITY
1
3




halt
EOFF

Where:

  • SuperUser - Is your username
  • superuserpassword - Is your SuperUser password

I have chosen SECURITY menu options 1, then option 3. Then I hit ENTER until I exited ^SECURITY routine and terminated the session with the halt command.

If you are using OS single sign-on, remove these two first lines since Caché won't ask for them.

The blank lines after number 3 are the ENTERs you enter to go up into the menu hierarchy until you exit.

The halt is necessary to avoid an error such as the following:

ERROR: <ENDOFFILE>SYSTEMIMPORTALL+212^SECURITY
%SYS>
<ENDOFFILE>
<ERRTRAP>

You can do more complex stuff with this technique such as validate errors and return unix error codes to your shell so that you can know if the operation was successful or not:

#!/bin/bash

csession INSTANCENAME <<EOFF
ZN "MYNAMESPACE"

Set tSC = ##class(SomeClass).SomeMethod()
If $System.Status.IsError(tSC) Do $System.Status.DisplayError(tSC) Do $zu(4,$j,1) ;Failure!

Do $zu(4,$j,0) ;OK!
EOFF

The $zu(4,$j,rc) will halt the session and return the return code on rc to your shell script. As you can notice, the Halt command is not necessary when using this $zu function.

I hope that helps!

Kind regards,

AS