ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Syntax to format text

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Syntax to format text

    I have a CLP that I'm trying to write a file to IFS. The file needs to have only LF at the end of each line. Not CRLF.
    The file needs to look like this:
    Code:
    #!/bin/sh
    printf sq9zp6r1M3xG005​
    At the moment the program is producing this:
    Code:
    " ""#!/bin/sh""                                                                                                "
    " printf sq9zp6r1M3XG005                                                                                     "​
    I need to remove all the quotes and extra spaces.

    The program is complicated. If there is an easier way to write the file I'm all ears.

    This is the program I have:
    Code:
    PGM        PARM(&USER &SITE &DIR &PWD &IMGCLG &LDIR)
    DCL &PWD       *CHAR 15
     DCL &Q         *CHAR 1  Value('''')                              
     DCL &LineOut   *CHAR 88                                          
     DCL &Preamble  *CHAR STG(*DEFINED) LEN(40) DEFVAR(&LineOut  1)    
     DCL &Linedata    *CHAR STG(*DEFINED) LEN(46) DEFVAR(&LineOut 42)  ​
    DCL        VAR(&Bash      ) TYPE(*CHAR) LEN(11) VALUE("#!/bin/sh")
    CRTSRCPF      FILE(QTEMP/FTP) RCDLEN(120) MBR(CMD)
     CHGVAR     VAR(&PREAMBLE) VALUE('Insert into QTEMP/FTP +  
                         (SRCDTA)  values(' || &Q)
    CHGVAR     VAR(&Linedata) VALUE(&BASH *TCAT &Q  || ')')
    RUNSQL     SQL(%TRIM(&LineOut)) COMMIT(*NONE) NAMING(*SYS)
    CHGVAR     VAR(&Linedata) VALUE('printf ' || &PWD *TCAT &Q *TCAT ')')        
    RUNSQL     SQL(&LineOut) COMMIT(*NONE) NAMING(*SYS)                ​
    CPYTOIMPF FROMFILE(QTEMP/FTP) +                      
    TOSTMF('/QOpenSys/etc/openssh_password_script.sh') +
    MBROPT(*REPLACE) +                                  
    RCDDLM(*LF)                                          ​
    ENDPGM

    The program creates a file in Qtemp called FTP
    The program writes records to the file
    Then the program copies the file to the IFS file
    When the program runs the variable &Linedata just before the RUNSQL line looks like this:
    Code:
     Insert into QTEMP/FTP   (SRCDTA)  values('' "#!/bin/sh"'')​

  • #2
    If you want to stick with the temporary file in QTEMP method, then some suggested changes are to create a regular physical file instead of a source file and use CPYTOSTMF instead of CPYTOIMPF. Here's an example program (note that multiple quotes in a row are all single quotes):
    Code:
    PGM
                 DCL                  VAR(&PWD) TYPE(*CHAR) LEN(15) +
                                             VALUE('sq9zp6r1M3xG005')
                 DCL                  VAR(&SQL) TYPE(*CHAR) LEN(1000)
                 DCL                  VAR(&FROMMBR) TYPE(*CHAR) LEN(200) +
                                             VALUE('/qsys.lib/qtemp.lib/ftp.file/ftp.mbr')
                 DCL                  VAR(&TOSTMF) TYPE(*CHAR) LEN(200) +
                                             VALUE('/QOpenSys/etc/openssh_password_script.sh')
    
                 CRTPF             FILE(QTEMP/FTP) RCDLEN(100)
    
                 CHGVAR          VAR(&SQL) VALUE('insert into FTP +
                                             values(''#!/bin/sh''), (''printf' *BCAT +
                                             &PWD *TCAT ''')')
                 RUNSQL          SQL(&SQL) COMMIT(*NONE)
    
                 CPYTOSTMF  FROMMBR(&FROMMBR) TOSTMF(&TOSTMF) +
                                            STMFOPT(*REPLACE) STMFCCSID(819) +
                                            ENDLINFMT(*LF)
    ENDPGM

    If you are on 7.3 or above and are at the correct TR level, then there is a way to write to a stream file directly from CL using the QSYS2.IFS_WRITE SQL procedure. Here's an example (again, multiple quotes in a row are all single quotes):
    Code:
    PGM
                 DCL                  VAR(&PWD) TYPE(*CHAR) LEN(15) +
                                             VALUE('sq9zp6r1M3xG005')
                 DCL                  VAR(&SQL) TYPE(*CHAR) LEN(1000)
                 DCL                  VAR(&TOSTMF) TYPE(*CHAR) LEN(200) +
                                             VALUE('/QOpenSys/etc/openssh_password_script.sh')
    
                 CHGVAR          VAR(&SQL) VALUE('Call QSYS2.IFS_Write( +
                                             Path_Name => ''' *CAT &TOSTMF *TCAT ''', +
                                             Line => ''#!/bin/sh'',                  +
                                             File_CCSID => 819, Overwrite => +
                                             ''REPLACE'', End_of_Line => ''LF'')')
                 RUNSQL          SQL(&SQL) COMMIT(*NONE)
    ​             CHGVAR          VAR(&SQL) VALUE('Call QSYS2.IFS_Write( +
                                             Path_Name => ''' *CAT &TOSTMF *TCAT ''', +
                                             Line => ''printf' *BCAT &PWD *TCAT ''', +
                                             File_CCSID => 819, Overwrite => +
                                             ''APPEND'', End_of_Line => ''LF'')')
                 RUNSQL          SQL(&SQL) COMMIT(*NONE)
    ​ENDPGM​

    Comment


    • #3
      Brian: You are a genius. That is really really great of you to take the time to create these for me.
      They are much simpler and the really great thing is they work. :-)
      Thank you very very much,

      Comment


      • #4
        Is there a way to get a joblog from this cmd that's run in a CLP at V7R4:
        Code:
         QSH        CMD('exec /QOpenSys/usr/bin/ksh -c  "/home/sftpuser/batch_sftp_script.sh "')​

        Comment

        Working...
        X