1

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
Michael Kristofik
  • 34,290
  • 15
  • 75
  • 125
Jason
  • 681
  • 1
  • 9
  • 19

3 Answers3

3

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.

Dave Smith
  • 726
  • 4
  • 7
  • Thanks, your second approach is what I was looking for. Something a little better than using files. – Jason Dec 15 '10 at 15:13
0

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

EvilTeach
  • 28,120
  • 21
  • 85
  • 141
0

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

Clarius
  • 1,183
  • 10
  • 10