shellsqlplustcsh

nested foreach in tcsh


I need to act over several database clients, these client have a static name APPUSR with numbers (1,11,12,13 2,21,22,23 3,31,32,33 ...) Id. APPUSR1,APPUSR12, APPUSR3, APPUSR32,....

I thought loop through the tens and other loop for units to create the connection name -

#!/usr/bin/tcsh

set echo

foreach envMast  ( `seq 4 ` )
  foreach envClient ( `seq 0 3 ` )     
if ( "$envClient" == 0) then         
  set envClient = ""   #there is not user 10,20,30 so i blank unit 0 to get envMast 1 2 3 4
endif

sqlplus APPUSR${envMast}${envClient}/APPUSR${envMast}${envClient}@DB <<-EOT>APPUSR.log
SELECT USER FROM DUAL;
exit
EOT
  end
end

It runs ok but only once for APPUSR1.
maybe a simple logic error (I'm newbie on tcsh) but have no idea why it didn't iterate or loop into any of the foreach.


Solution

  • Ignoring the sqlplus command, your nested foreach is built correctly.

    The problem you're seeing is because your "here document" between <<EOT and EOUT is not working inside the foreach loop. This doesn't work in tcsh. (see Bugs section https://linux.die.net/man/1/tcsh)

    You'll need to find a way to construct your sqlplus command differently. Maybe you could move those lines into a different file and then source the file from your csh script, like this:

    foreach envMast  ( `seq 4 ` )
        foreach envClient ( `seq 0 3 ` )     
            if ( "$envClient" == 0) then         
                set envClient = ""   
            endif
    
            source sqlcmd.csh >> APPUSR.log
    
        end
    end