contact image

Qshell

Qshell is a command environment based on POSIX and X/Open standards. It consists of two parts:

  1. The shell interpreter (or qsh) is a program that reads commands from an input source, interprets each command, and then runs the command using the services of the operating system.
  2. The utilities (or commands) are external programs that provide additional functions and can be quite simple or very complex.


Post Your Example

Send a message to QHST log from program using QSHELL. Also example of parsing .csv using strtok.


Sample from Jamie Flanary posted at 2012-01-14 12:46:32

     h dftactgrp(*no) actgrp(*caller) option(*srcstmt)                                              
     h bnddir('QC2LE')                                                                              
                                                                                                    
     ffb01testp if   e           k disk    rename(fb01testp:fb01testr)                              
                                                                                                    
     d cmdlength       s             15  5                                                          
     d cmdstring       s            512                                                             
     d count           s              3s 0                                                          
     d counter         s              3s 0                                                          
     d DQ              s              1    inz('"')                                                 
     d element         s              3s 0                                                          
     d TableHeading    s             25                                                             
     d isodate         s               d   inz                                                      
     d mycounter       s              3  0 inz                                                      
     d n12             s             12s 0                                                          
     d pointer         s               *                                                            
     d Q               s              1    inz('''')                                                
     d reply           s              1                                                             
     d response        s           4096a                                                            
     d rundte          s              6s 0                                                          
     d StopProcessing  s               n                                                            
     d  token          S            160A   varying                                                  
                                                                                                    
     dstrtok           PR              *   ExtProc('strtok')                                        
     d string                          *   value options(*string)                                   
     d delim                           *   Value Options(*string)                                   
                                                                                                    
     d Frost           ds                  qualified  inz                                           
     d  WholeHeader                                                                                 
     d   DataString                  27    dim(32) overlay(WholeHeader:*next)                       
     d   Title                       25    overlay(dataString:1)                                    
     d   OriginalOrder...                                                                           
     d                                2  0 overlay(datastring:*next)                                
                                                                                                    
     d $command        pr                  extpgm('QCMDEXC')                                        
     d   command                    512                                                             
     d   Length                      15  5                                                          
                                                                                                    
                                                                                                    
      /free                                                                                         
                                                                                                    
         exsr $OriginalHeader;                                                                      
                                                                                                    
         setll *start fb01testp;                                                                    
         read fb01testp;                                                                            
         dow not %eof(fb01testp);                                                                   
                                                                                                    
          response  =  srcdta;                                                                      
                                                                                                    
        //response = %scanrpl(',,' : ', ,' : response);                                             
                                                                                                    
          dow (%scan(',,': response) > 0);                                                          
           response = %replace(' ': response: %scan(',,': response) + 1: 0);                        
          enddo;                                                                                    
                                                                                                    
        //‚find the header to validate.. If NO header then validate field count ??                  
           if   %scan('Report Date':response) > *zeros;                                             
            exsr $CompareHeaders;                                                                   
           endif;                                                                                   
                                                                                                    
                                                                                                    
          read fb01testp;                                                                           
         enddo;                                                                                     
                                                                                                    
         *inlr = *on;                                                                               
                                                                                                    
           //------------------------------------------------                                       
           // $CompareHeaders - validate the inbound data b4                                        
           //                   processing                                                          
           //------------------------------------------------                                       
                                                                                                    
             begsr $CompareHeaders;                                                                 
                                                                                                    
              reset counter;                                                                        
              reset StopProcessing;                                                                 
              pointer = strtok(response: ',');                                                      
                                                                                                    
              dow (pointer <> *null);                                                               
               counter+=1;                                                                          
               token = %trim(%str(pointer));                                                        
               pointer = strtok(*null: ',');                                                        
               TableHeading = %trim(token);                                                         
                                                                                                    
               Element = %lookup(%trim(TableHeading):frost.title);                                  
                                                                                                    
               if Element = *zeros or      // *New Field added                                      
                  Element <> frost.OriginalOrder(Element);  // Order of fields Changed              
                StopProcessing = *on;                                                               
                                                                                                    
                // Put together string to send to QSHT messageQ                                     
                  cmdstring = 'qsh cmd(' + Q + ' logger -t ' + DQ + 'FB01TEST' +                    
                              DQ + ' ' + DQ ;                                                       
                select;                                                                             
                 when Element = *zeros;  // new field entered or heading added                      
                  cmdstring = %trim(cmdstring) +                                                    
                   'New field added to FROST item download' + DQ + ' ' + Q +')';                    
                  cmdlength = %len(%trim(cmdstring));                                               
                  monitor;                                                                          
                   $command(cmdstring:cmdlength);                                                   
                  on-error;                                                                         
                  endmon;                                                                           
                                                                                                    
                 other;  // Order of fields has changed                                             
                  cmdstring = %trim(cmdstring) +                                                    
                   'The order of downloaded fields has changed.' +                                  
                   DQ + ' ' + Q +')';                                                               
                  cmdlength = %len(%trim(cmdstring));                                               
                  monitor;                                                                          
                   $command(cmdstring:cmdlength);                                                   
                  on-error;                                                                         
                  endmon;                                                                           
                                                                                                    
                endsl;                                                                              
                                                                                                    
                leave;                                                                              
               endif;                                                                               
                                                                                                    
              enddo;                                                                                
                                                                                                    
             endsr;                                                                                 
                                                                                                    
           //-------------------------------------------                                            
           // $OriginalHeader - load current header                                                 
           //-------------------------------------------                                            
                                                                                                    
             begsr $OriginalHeader;                                                                 
                                                                                                    
              reset MyCounter;                                                                      
                                                                                                    
              for count = 1 to %elem(Frost.datastring);                                             
               frost.originalorder(count) = count;                                                  
               select;                                                                              
                when count = 1;                                                                     
                 frost.title(count) = 'Report Date';                                                
                when count = 2;                                                                     
                 frost.title(count) = 'Report Requestor';                                           
                when count = 3;                                                                     
                 frost.title(count) = 'Report Request User ID';                                     
                when count = 4;                                                                     
                 frost.title(count) = 'Location Name';                                              
                when count = 5;                                                                     
                 frost.title(count) = 'Location Code';                                              
                when count = 6;                                                                     
                 frost.title(count) = 'Deposit Account Name';                                       
                when count = 7;                                                                     
                 frost.title(count) = 'Deposit Account';                                            
                when count = 8;                                                                     
                 frost.title(count) = 'Submit Date';                                                
                when count = 9;                                                                     
                 frost.title(count) = 'Post Date';                                                  
                when count = 10;                                                                    
                 frost.title(count) = 'Deposit Number';                                             
                when count = 11;                                                                    
                 frost.title(count) = 'Item Type';                                                  
                when count = 12;                                                                    
                 frost.title(count) = 'Item Status';                                                
                when count = 13;                                                                    
                 frost.title(count) = 'Sequence Number';                                            
                when count = 14;                                                                    
                 frost.title(count) = 'User Name';                                                  
                when count = 15;                                                                    
                 frost.title(count) = 'User Login ID';                                              
                when count = 16;                                                                    
                 frost.title(count) = 'EPC';                                                        
                when count = 17;                                                                    
                 frost.title(count) = 'R/T';                                                        
                when count = 18;                                                                    
                 frost.title(count) = 'Account Number';                                             
                when count = 19;                                                                    
                 frost.title(count) = 'Post Amount';                                                
                when count = 20;                                                                    
                 frost.title(count) = 'Adjustment Amount';                                          
                when count = 21;                                                                    
                 frost.title(count) = 'Credit Amount';                                              
                when count = 22;                                                                    
                 frost.title(count) = 'Difference Amount';                                          
                when count = 23;                                                                    
                 frost.title(count) = 'Check Number';                                               
                when count = 24;                                                                    
                 frost.title(count) = 'Item Id';                                                    
                when count = 25;                                                                    
                 frost.title(count) = 'Bank Item Sequence #';                                       
                when count = 26;                                                                    
                 frost.title(count) = 'Adjustment Reason Code';                                     
                when count = 27;                                                                    
                 frost.title(count) = 'MICR';                                                       
                when count = 28;                                                                    
                 frost.title(count) = 'Policy Number';                                              
                when count = 29;                                                                    
                 frost.title(count) = 'Payor Name';                                                 
                when count = 30;                                                                    
                 frost.title(count) = 'Payor Code';                                                 
                when count = 31;                                                                    
                 frost.title(count) = 'Policy ID';                                                  
                when count = 32;                                                                    
                 frost.title(count) = 'Check';                                                      
               endsl;                                                                               
              endfor;                                                                               
                                                                                                    
             endsr;                                                                                 
                                                                                                    
                                                                                                    
      /end-free                                                                                     

Change the date and time of all objects in a folder on the IFS.


Sample from Jamie Flanary posted at 2011-12-12 18:27:28

STRQSH CMD('touch  -t  200712250000.00 /home/flanary/test.doc')  

A much easier way to zip *ALL documents in one folder on the IFS.


Sample from Jamie Flanary posted at 2011-12-12 18:15:04

I have 4 .csv files created in QTEMP
I need to email these to the user that
just created them....

created folder on root called ZIP
(this works fine for me cause Im the only one doing this)
You will need to create subfolders by job to release this
to the hounds.
PHP Code:
MD DIR(zip) DTAAUT(*RWX) OBJAUT(*ALL)  

In my case I just ran 4 cpytoimpf commands


PHP Code:
CPYTOIMPF FROMFILE(QTEMP/WKR20WORK1) TOSTMF('zip/wkr20work1.csv')
STMFCODPAG(*PCASCII) RCDDLM(*CRLF)      

CPYTOIMPF FROMFILE(QTEMP/WKR20WORK2) TOSTMF('zip/wkr20work2.csv')
STMFCODPAG(*PCASCII) RCDDLM(*CRLF)      

CPYTOIMPF FROMFILE(QTEMP/WKR20WORK3) TOSTMF('zip/wkr20work3.csv')
STMFCODPAG(*PCASCII) RCDDLM(*CRLF)      

CPYTOIMPF FROMFILE(QTEMP/WKR20WORK4) TOSTMF('zip/wkr20work4.csv')
STMFCODPAG(*PCASCII) RCDDLM(*CRLF)  


Then to get them to the zip file......named profit.zip

PHP Code:
STRQSH CMD('jar cfM zip/profit.zip zip/*.*')  

*** Note dont run this I think it would zip the entire IFS into a zip file
that can never turn out GOOD! 

PHP Code:
STRQSH CMD('jar cfM zip/profit.zip *.*')  

anyway then you have your files all zipped up and
can send them to anyone you wish with your favorite 
email process. (there are a couple posted on the site)

Using QSHELL to zip and unzip objects in the IFS


Sample from Jamie Flanary posted at 2011-12-12 18:04:00

Here are the steps to add the Unzip and Zip functions to your System i server: 
Go to the following File Transfer Protocol (FTP) site and download the archives:

http://www.ctan.org/tex-archive/tool...-zip/UNIX/AIX/
In our case, we download these files:

zip23x-aix43.zip To compress (zip) files
unz550x-aix5L.tar.gz To uncompress (unzip) files
Place these files somewhere on your System i server. In this example, we use /home/zip. 
You will also need the gunzip tool to uncompress the unz550x-aix5L.tar.gz file. Download the tool from the following Web site:
http://www-03.ibm.com/servers/enable...utilities.html

Place the file in the same directory as the other two files.
Review the license agreement for the downloaded tools.
Sign on to your System i server and start an i5/OS PASE terminal with the command:


CALL QP2TERM

Change to the /home/zip directory:



cd /home/zip

The unzip file is a compressed file. Uncompress this file:



uncompress gzip.1.2.4a.tar.Z

Untar” the gzip tar file:



tar -xvf gzip.1.2.4a.tar

It installs the executable files under the /usr/local/bin directory.


Update the PATH variable with the /usr/loca/bin directory by running this command:


export PATH=$PATH:/usr/local/bin

Uncompress the unz550x-aix5L.tar.gz file using this command:



gunzip unz550x-aix5L.tar.gz

“Untar” the uncompressed file:



tar -xvf unz550x-aix5L.tar

It creates the directory unzip-5.50.
Change to this directory:



cd unzip-5.50

Enter the following command:



 unzip

It shows you the command usage text.


To run the command from any directory and any terminal window, you have to copy the unzip file into the /QOpenSys/usr/bin directory: 


cp unzip /QOpenSys/usr/bin


Switch back to the directory to which you have downloaded the files:



cd /home/zip


Now check whether unzip is really working. The Zip program on our system is packed in the zip file - zip23x-aix43.zip, so we can try on that one. Verify that you are in the directory in which the zip23x-aix43.zip file is located. Enter the following command:


unzip -d ./zip zip23x-aix43.zip


This creates a directory named zip and places all files into that directory. Change to this directory:


cd zip


Issue the following command to see if the program is working:

 zip
 

This will show the usage notes.
To make the command available in every i5/OS PASE terminal session, copy the program into the /QOpenSys/usr/bin directory.


  cp zip /QOpenSys/usr/bin
  

To use both commands (zip and unzip) in QShell, make another copy of the programs into the /usr/bin directory by running 2 commands:


cp /QOpenSys/usr/bin/zip /usr/bin 
cp /QOpenSys/usr/bin/unzip /usr/bin


Now zip and unzip are available for the next QShell terminal.

Changing CCSID of IFS table


Sample from Scott Klement posted at 2011-12-11 10:45:27

Another way to do the same thing is with the SETCCSID command in QShell. 
Here's an example of that command:

STRQSH CMD('setccsid 819 /path/to/myfile.txt')
If the data in the stream file also needs to be converted, you can use QShell's 
ICONV command to convert it. This does not set the CCSID of the resulting file 
properly, but we already know how to fix that!

The following command converts the data in MYFILE.TXT from 
CCSID 37 to CCSID 819 and writes the output to YOURFILE.TXT:

STRQSH CMD('iconv -f 37 -t 819 /path/to/myfile.txt > /path/to/yourfile.txt 
&& setccsid 819 /path/to/yourfile.txt')

program reads STDIN and translates all of the * spaces to periods


Sample from Scott Klement posted at 2011-12-11 10:41:17

     * This rather silly program reads STDIN and translates all of the
      * spaces to periods, then writes it to STDOUT.
      *
      * You need to run this from QSHELL for it to be useful:
      *          /qsys.lib/yourlib.lib/this.pgm

     H BNDDIR('QC2LE') DFTACTGRP(*NO)

     D read            PR            10I 0 extproc('read')
     D  fd                           10I 0 value
     D  buf                            *   value
     D  len                          10I 0 value

     D write           PR            10I 0 extproc('write')
     D  fd                           10I 0 value
     D  buf                            *   value
     D  size                         10I 0 value

     D data            S             80A
     D len             S             10I 0

     c                   eval      len = read(0: %addr(data): %size(data))
     c                   dow       len > 0
     c     ' ':'.'       xlate     data          data
     c                   callp     write(1: %addr(data): len)
     c                   eval      len = read(0: %addr(data): %size(data))
     c                   enddo

     c                   eval      *inlr = *on


To run this, you need to run it from QSHELL.  Here's how I did it:

1) Type STRQSH

2) At the QSHELL prompt, I typed:

echo "I sure wish I had periods in this text!" | /qsys.lib/mylib.lib/test.pgm

(that should all be one line, if it gets wrapped)


And the result looked like this:

   I.sure.wish.I.had.periods.in.this.text!

CL Program using Shell command ls to dump directory listing to a file


Sample from Jamie Flanary posted at 2011-12-11 10:33:11

PGM                                                         
                                                            
CD         DIR('/myxml/data')                               
CRTPF      FILE($library/ifsdir) RCDLEN(128)                     
STRQSH     CMD('ls -lt *.* > +                              
             /qsys.lib/$library.lib/ifsdir.file/ifsdir.mbr')     
                                                            
ENDPGM