Tip

Ways to put QSHELL to work in your day-to-day programming life

I have been reading a lot about QSHELL since it was introduced for the iSeries. After learning about this shell I started promoting this piece of technology to my colleagues. Time and time again I was asked, "What is the usage of this technology when most of the functionalities can be done with CL or RPG?" Due to the lack of shell commands at that time, I restricted my answer to, "It helps in compiling Java programs." At that time, Java was an unusual language for iSeries users.

I have since discovered that QSHELL has become much more. It is now the equivalent of any other UNIX shell. However, now I'm asked, "Why should we adopt UNIX, when we have such a good operating system as OS/400 and the advance scripting language CLLE?" The answer is that QSHELL helps CL rather than competes with it. Here are few ways that you can use QSHELL in your day-to-day life of programming.

Usage for database operation

CL has a major restriction of using Data Manipulation Language (DML) such as Update, insert or selected delete with the DB2/400 database. To achieve this, we use roundabout ways such as RUNSQLSTM, writing new RPG program, etc.

Qshell makes life easier by using the following command:
Syntax: QSH CMD('db2 "<Db2/400 sql statement">)

Example:
QSH CMD('db2 "UPDATE MYLIB.MYFILE SET MYFLD = ''P''" ')
QSH CMD('db2 "INSERT INTO MYLIB.MYTABLE VALUES(''jagannath'', 104.20)"')

Things to note:

  1. The query naming convention is SQL not SYS. The table/file is qualified with Library.Table not Library/Table.
  2. The QSH command cannot access the objects, which are created in QTEMP using command line or CL programs. That's because shell commands are executed in a separate job other than the parent jobs. These jobs are triggered automatically when QSH command is executed.

    If there is a file as QTEMP/MYFILE and I want to update it using this QSH command
    QSH CMD('db2  "UPDATE QTEMP.MYFILE SET MYFLD = ''P''" ')  
     
    It will generate an error message such as this one:

     
    NATIVE ERROR CODE: -204              
    MYFILE in QTEMP type *FILE not found.
    Press ENTER to end terminal session. 
    

Usage for IFS operation

It is always a headache to handle IFS files using RPG programs. We have to write separate RPG programs using C APIs to handle this functionality. But QShell commands ease this process.

Syntax:

To replace the contents of the IFS and write the fresh text
QSH CMD('print It is working > /home/mydir/myfile.txt')

To append to the contents of the IFS file in next line
QSH CMD('print It is working >> /home/mydir/myfile.txt')

To use other features of print, go through the QSHELL manual of IBM.

I found this functionality helped me while creating CSV files out of database files using CPYTOIMPF. With this command we create the comma separated files out of the database file. To add the report headings, field descriptions, etc., the print command helps a lot.

Example:

QSH CMD('print ", , ,Report for  Health Care," > /home/mydir/myexport.csv')
QSH CMD('print "Patient,Date of Adm,Doctor Attended, Remarks" >> /home/mydir/myexport.csv')
CPYTOIMPF FROMFILE(MYLIB/HLTHCAR) TOSTMF('/home/jlenka/myexport.csv') MBROPT(*ADD) RCDDLM(*CRLF)                                                       

Redirecting

The output of the QSHELL commands is normally displayed in the standard output. (In UNIX terms, standard output (screen) is also called as a kind of file.) However, we can redirect this output to an IFS file. Since regular programmers are more acquainted with DB2/400 files, let's see how we can redirect the output to the DB2/400 files.

Let's first look at the mapping of the naming conventions between OS/400 and IFS.

Sl No OS/400 Naming Convention IFS Naming Convention
10 MYLIB/MYFILE MYMBR /QSYS.LIB/MYLIB.LIB/MYFILE.FILE/MYMBR.MBR
20 MYLIB/MYSAVF (Save File) /QSYS.LIB/MYLIB.LIB/MYSAVF.SAVF
30 MYLIB/MYFILE Program /QSYS.LIB/MYLIB.LIB/MYPGM.PGM

In QSHELL 'ls' is used similarly to WRKOBJ *ALL in OS/400. If you want to redirect the output result of ls to a DB2/400 file, do the following:

  1. CRTPF FILE(MYLIB/MYFILE) RCDLEN(300)
  2. QSH cmd('ls /qsys.lib/mylib.lib/* > /qsys.lib/myfile.lib/myfile.file/myfile.mbr')

Searching objects

If I want find the name of the file that ends with "PGL" in MYLIB library, what should I do? WRKOBJ command does not support WRKOBJ *PGL. We can use one of the versatile file searching utilities called find.

  1. CRTPF FILE(MYLIB/OUTPUT) RCDLEN(300)
  2. QSH CMD(find /qsys.lib/MYLIB.LIB -name '*PGL.FILE > /qsys.lib/mylib.lib/output.file/output.mbr')

Things to note:

  • The searching pattern (e.g.,*PGL.FILE) must be in capital letters
  • You can do lots of wild card searches
  • Example:

    1. Starting with A ending with X , find /qsys.lib/MYLIB.LIB -name 'A*B.FILE'
    2. Starting with A and in between CD, find /qsys.lib/MYLIB.LIB -name 'A*CD*.FILE'
    3. Search in library that starts with JA and JL,
      find /qsys.lib/JL*.LIB /qsys.lib/JA*.LIB -name 'XML*.FILE'

  • You can search for objects created by JLENKA
    find /qsys.lib/JL*.LIB /qsys.lib/JA*.LIB -user 'JLENKA'
  • You can search for programs created by JLENKA
    find /qsys.lib/JL*.LIB /qsys.lib/JA*.LIB -user 'JLENKA' –name '*.PGM'
    i.e. qsh cmd('find /qsys.lib/JL* -user ''JLENKA'' -name ''*.PGM''')
  • Remember to use two quote marks while taking the shell commands under the CMD parameter for the strings where one quote is used. For example, in the above command ''JLENKA'' was used when putting it under QSH.

Counting the results

Want to count how many programs are in one library? You could do a WRKOBJ and then transfer to a file and use query. But it can be done using wc:
Qsh CMD('find /qsys.lib/MYLIB.LIB -name ''*'' | wc -l')

Searching strings

The only way to find strings or a pattern of strings in the members of the source physical file is with the FNDSTRPDM command. This command has lots of restrictions, such as it does not let you search more than one pattern (string) at one time. One of the best searching utilities in the industry is grep, which is used in UNIX shells. Let's see how we can use it to replace FNDSTRPDM.

Example:

  1. 1. To search a string "it is ok" in MYLIB/QRPGLESRC and redirect to a file with the text lines containing this string:
    QSH CMD('grep ''it is ok'' /qsys.lib/jlenka.lib/qrpglesrc.file/* >/qsys.lib/jlenka.lib/myfile.file/myfile.mbr')
  2. To search a string "it is ok" in MYLIB/QRPGLESRC and redirect to a file and only show the file names no text lines:
    QSH CMD('grep –l ''it is ok'' /qsys.lib/jlenka.lib/qrpglesrc.file/* >/qsys.lib/jlenka.lib/myfile.file/myfile.mbr')
  3. To search a string "it is ok" or 'it is correct" in MYLIB/QRPGLESRC and redirect to a file:
    QSH CMD('egrep –l ''it is ok|it is correct'' /qsys.lib/jlenka.lib/qrpglesrc.file/* >/qsys.lib/jlenka.lib/myfile.file/myfile.mbr')
  4. . To search a string "it is ok" and "it is correct" in MYLIB/QRPGLESRC in one line and redirect to a file:
    qsh cmd('grep ''IFO'' /qsys.lib/mylib.lib/qrpglesrc.file/* |
    grep ''and'' >
    /qsys.lib/jlenka.lib/myfile.file/myfile.mbr')

More you can do

There are many other useful commands that can help you program lots of complicated stuff in CL using QSH. Try going through Rfile to read or write to the OS/400 files or jar to zip the IFS files to transmit them by mail. Explore the other QSH commands in the iSeries Infocenter.

---------------------------
About the author: Jagannath Prasad Lenka is a programmer/analyst at Infosys Technologies Ltd. He has worked as programmer and senior programmer on the iSeries for this company for the past five years.


This was first published in September 2005

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.