IBM Support

Redirecting Output in Qshell

Troubleshooting


Problem

This document discusses techniques to redirect output in Qshell.

Resolving The Problem

Normally, the output of utilities and programs run in Qshell will be displayed on the terminal device. That output was sent to either the standard output device (stdout) or the standard error device (stderr). There are times when that output should be saved to a file for debug purposes, as a log, or to use as input for another program or utility later. There are two different redirection operators to direct output to a file: > and >>. The single greater than symbol will direct the output to a file, overwriting anything that might have previously been in the file. Two greater than symbols will direct the output to a file, appending the data to the end of the file if it already exists. As an example, if you were to run these two commands:

ls /QIBM > /temp/myoutput.txt
system "dspusrprf quser output(*print)" > /temp/myoutput.txt

The file, /temp/myoutput.txt will only contain the output of the dspusrprf command because the > redirect function will overwrite anything already in the file. If you ran the commands using the >> redirector, the myoutput.txt file would contain the output of both commands.

When you redirect output using > or >>, you are redirecting stdout to another device (almost always a file). Many commands and utilities will also make use of the stderr device and that output would not be redirected by simply using the > or >> redirection. In order to get the stderr output into a file, you need to modify the redirection command by adding the standard descriptor number for stderr, which is 2. As an example, the 'system' utility in Qshell will direct messages from the command to the stderr device. Any output that normally would go to a spool file is directed to stdout. In the example above, the output of the dspusrprf command is directed to stdout, and redirected to a streamfile. A command like CRTSAVF would normally just return messages saying that it was created or that it could not be created. When the messages are not redirected, they just appear on the screen. When you redirect that output, you can tell that it was not going to stdout. For example, running the following in qshell:

system "crtsavf qgpl/mysavf" > /temp/myoutput.txt

will produce the following message in the terminal:

CPC7301: File MYSAVF created in library QGPL.

The output was not redirected to the file because the output was sent to stderr and not to stdout. If the command is modified to redirect stderr to the streamfile like this:

system "crtsavf qgpl/mysavf" 2> /temp/myoutput.txt

then the output of the command is directed into the streamfile.

What if you want to direct both stdout and stderr to a device? That is where it gets a little more complicated. The Qshell environment uses the same syntax as DOS does, rather than the standard BASH & redirection. In order to get both stdout and stderr directed to a device, you first direct stdout to the device using > and then redirect stderr to stdout (2>&1). This may sound very confusing; however, an example or two should make it more clear. You can direct the output of both stdout and stderr into a file, appending the output of the second command to the first:

system "crtsavf qgpl/mysavf" > /temp/myoutput.txt 2>&1
system "dspsavf qgpl/mysavf output(*print)" >> /temp/myoutput.txt 2>&1

[{"Type":"MASTER","Line of Business":{"code":"LOB68","label":"Power HW"},"Business Unit":{"code":"BU070","label":"IBM Infrastructure"},"Product":{"code":"SWG60","label":"IBM i"},"ARM Category":[{"code":"a8m0z0000000CHQAA2","label":"Qshell"}],"ARM Case Number":"","Platform":[{"code":"PF012","label":"IBM i"}],"Version":"All Versions"}]

Historical Number

525615396

Document Information

More support for:
IBM i

Component:
Qshell

Software version:
All Versions

Operating system(s):
IBM i

Document number:
634541

Modified date:
15 November 2024

UID

nas8N1012920

Manage My Notification Subscriptions