It's Easy to Run PHP, QShell, and PASE Tools from CL, Too!

Article ID: 58758

A couple of months ago, I wrote about and published a utility to make it easy to run Unix commands. This includes all manner of QShell and PASE utilities, including PHP, SSH, IFS utilities, and so forth. The goal of the utility was to make it so easy to run these sorts of commands that you wouldn't hesitate to use them in your programs.

Those articles focused on RPG, but today I'm taking this utility one step further and making it available to CL programs.

The Gist

More and more software is becoming available on IBM i through the Unix interfaces of QShell and PASE. These environments also provide a ton of utilities, which are often very useful.

In the Unix world, utilities can get their input from a user, a file, or another program. When utilities read their input, by default, it comes from the user's keyboard. But specifying "redirection" instructs the utility to get its input from a file, and specifying a "pipe" instructs the utility to get its input from another program. Likewise, a utility's output goes to the screen by default, but it can be redirected to a file or piped to a program.

For example:

ls > /tmp/example.txt

The ls command lists the files in a directory. The > character is a redirection operator that tells QShell to send the output to a file instead of printing it on the screen. The file is named /tmp/example.txt. You might think of redirection as being similar to the OVRxxx commands in the native environment... it overrides the output of the ls command to go to a file.

A pipe would work like this instead:

ls | /path/to/program

So the vertical line is a pipe, and the > symbol is a redirection. There are also pipes and redirection operators that control where a program's input is from.

IBM i provides APIs that let you build and run your own Unix pipes in the native environment. The only problem is that they're complex to use. So the UNIXCMD project aims to make it easy to launch a Unix command and attach it to your RPG or CL program with a pipe. That way, you can read the command's output, or you can write data to its input. For RPG, I decided to use SPECIAL files, since things very easy. You write to the file using standard RPG WRITE opcodes and read from it using the standard RPG READ opcode.

Here are links to the previous articles about working with Unix commands from RPG:

Running Unix Commands from CL

It's true that CL already has the STRQSH command and the CALL QP2SHELL API to make it relatively easy to call QShell or PASE tools. So why do you need my utility? Because connecting to the actively running process is simpler. You don't have to create a temporary file, fill it, and then read it back. You can just read the output of an IFS command as it's being run.

For example, perhaps you want to get a list of all of the PDF files in the /pdfs directory of the IFS, while ignoring other documents.

PGM
     DCL VAR(&LINE) TYPE(*CHAR) LEN(1000)
     DCL VAR(&EOF) TYPE(*LGL) VALUE('0')
     OPNPIPE CMD('cd /pdfs; ls')
 
     RCVPIPE RCD(&LINE) EOF(&EOF)
     DOWHILE (*NOT &EOF)
        // do something with filename here...
        RCVPIPE RCD(&LINE) EOF(&EOF)
     ENDDO

     CLOPIPE
ENDOGM

The Open Pipe (OPNPIPE) command launches a new Unix process running as a separate job. The process is connected to its caller with pipes that are set up for you by the OPNPIPE command. The pipe is intended to look as much like a file as possible. The OPNPIPE command opens it just as OPNDBF might open a file. The RCVPIPE command is used to read from the pipe in the same fashion that RCVF might read from a file. There's also a SNDPIPE command (not shown above) that can be used to write to the pipe. The CLOPIPE command closes the pipe.

The preceding code demonstrates reading a list of files from an IFS directory, using wildcards. With this new utility, it's really that easy to read the contents of an IFS directory in CL!

Conclusion

I've included only one example in this article, but there are many examples in the RPG version of this utility. (See the links above for more information.) It should be a simple matter to use the same Unix command strings from the RPG examples in a CL program, using the techniques shown above.

You can download the UNIXCMD utility, including both the RPG and CL interfaces from my website at the following link:
scottklement.com/unixcmd.

ProVIP Sponsors

ProVIP Sponsors