2015/05/21

Playing with the PIPE command

If you want to do something with het output of e.g. the SEARCH command, without creating intermediate files, you can use the PIPE command. One way to do this, is using 2 scripts, but it requires the second script to check the existance of SYS$PIPE.

The scripts below do nothing useful, but are just for demonstration.

DO_SOMETHING.COM

$ IF P1 .EQS. ""
$ THEN
$     WRITE SYS$OUTPUT "NO PARAMETER"
$ ELSE
$     PIPE DIR /SIZE/DATE/OWNER | -
           SEARCH SYS$PIPE 'P1' | -
           @DO_SOME_MORE | -
           TYPE SYS$PIPE > SOME_OUTPUT.TXT
$ ENDIF
$ EXIT

DO_SOME_MORE.COM

$ IF F$TRNLNM( "SYS$PIPE" ) .EQS. "" 
$ THEN
$     WRITE SYS$OUTPUT "NO PIPE"
$ ELSE
$read_loop:
$     READ /END=end_loop /ERROR=end_loop SYS$PIPE input_line
$     WRITE SYS$OUTPUT F$ELEMENT( 0, ";", input_line )
$     GOTO read_loop
$end_loop:
$ ENDIF
$ EXIT

You can do this with just one DCL script, which contains a PIPE command, but runs itself again within the PIPE command to process the output of a previous command.


DO_SOMETHING.COM

$ IF P1 .EQS. ""
$ THEN
$     WRITE SYS$OUTPUT "NO PARAMETER"
$     EXIT
$ ENDIF
$ IF P1 .EQS. "CALL$SELF"
$ THEN
$     CALL do_some_more
$ ELSE
$     do_some_more := @'F$ENVIRONMENT( "PROCEDURE" )' "CALL$SELF"
$     PIPE DIR /SIZE/DATE/OWNER | -
           SEARCH SYS$PIPE 'P1' | -
           do_some_more | -
           TYPE SYS$PIPE > SOME_OUTPUT.TXT
$ ENDIF
$ EXIT
$do_some_more: SUBROUTINE
$read_loop:
$     READ /END=end_loop /ERROR=end_loop SYS$PIPE input_line
$     WRITE SYS$OUTPUT F$ELEMENT( 0, ";", input_line )
$     GOTO read_loop
$end_loop:
$ ENDSUBROUTINE