How to assign the output of a program to a variable in a DCL com script on VMS?

2.1k Views Asked by At

For example, I have a perl script p.pl that writes "5" to stdout. I'd like to assign that output to a variable like so:

$ x = perl p.pl ! not working code
$ ! now x would be 5
3

There are 3 best solutions below

1
On BEST ANSWER

The PIPE command allows you to do Unix-ish pipelining, but DCL is not bash. Getting the output assigned to a symbol is tricky. Each PIPE segment runs in a separate subprocess (like Unix) and there's no way to return a symbol from a subprocess. AFAIK, there's no bash equivalent of assigning stdout to a variable.

The typical approach is to write (redirect) the output to a file and then read it back:

 $ PIPE perl p.pl > temp.txt 
 $ open t temp.txt
 $ read t x
 $ close t

Another approach is to assign the return value as a JOB logical which is shared by all subprocesses. This can be done as a one-liner using PIPE:

 $ PIPE perl p.pl | DEFINE/JOB RET_VALUE @SYS$PIPE
 $ x = f$logical("RET_VALUE")

Since the "RET_VALUE" is shared by all processes in the job, you have to be careful of side-effects.

0
On

I wanted to identify a particular ACE from a file's ACL and then assign the value to a variable I could refer to later in the script. I wanted to avoid the overhead of writing to/reading from a file as I had 1000s of files to iterate over. This method worked for me.

$ PIPE DIR/SEC filename | SEARCH SYS$PIPE variable | (READ SYS$PIPE variable && DEFINE/JOB/NOLOG variable &variable)

$ SHOW LOGICAL variable

0
On

Look up the PIPE command. It lets you do unix like things.