Problem:
This KB and demonstration joined was tested with Net Express Websync3 and Server Express 5.0 Websync 3 on AIX ( Server Express 5.0 WS3 PRN: RXCAM/AAI:9l.N6.50.03)
...................................
How to set Environment Variables in COBOL and retrieve them to a shell ( .bat | .sh ) ?
This is quite a common question asked more times to MF Supportline
...................................
When you use the syntax
working-storage section.
01 ENV-value pic x(200).
01 ENV-name pic x(200) .
Procedure division
*>
SetEnvtVar.
*--> set the value of environment name
display ENV-name upon environment-name
display ENV-value upon environment-value
perform GetEnvtVar
.
GetEnvtVar.
*--> read the value of environment variable
display ENV-name upon environment-name
accept ENV-value from environment-value.
.
You can easily READ
The content of environment variables set prior to the COBOL process to be launched (paragraph above: GetEnvtVar)
You can easily SET
Environment variables within COBOL
(paragraph above: SetEnvtVar)
And read the content you have just set with (paragraph above: GetEnvtVar)
within the process ( in main program or sub-programs )
...................................
...................................
BUT: How can an environment variable set in the COBOL program be retrieved to the Shell?
There is no direct solution to do this since
The life duration of the Environment variable you set within a process is the life duration of this process
I.e.: when you are back to the shell,
The environment variable you set in the process ( COBOL one or whatever ) doesn't exist anymore....
...................................
?? How to proceed then??
...................................
Resolution:
The demonstration attached proposes 2 scenarios
Both were tested on Net Express 5.0 SP3 and on Server Express 5.0 SP3 ( Server Express 5.0 WS3 PRN: RXCAM/AAI:9l.N6.50.03)
...................................
...................................
Scenario0:
======
Files used:
CL-SetEnvVar.bat ( on Net Express )
or
CL-SetEnvVar.sh ( on Server Express )
COBOL program setenvvar.cbl
The script above
........Sets the Environment variable SetEnvCmdFile
........Compiles setenvvar.cbl
........Launches setenvvar.int
................setenvvar.int
........................retrieves the content of the environment variable SetEnvCmdFile
........................OPENs OUTPUT a line sequential file using a dynamic assign
................................ ( File assigned = content of Envt Var SetEnvCmdFile
........................Write records to this file
................................On Windows: SET
EnvtVarXX= EnvtVarXX_Content
................................On UNIX
EnvtVarXX= EnvtVarXX_Content
........................................................export EnvtVarXX
........................CLOSEs the file
........Launches the scriot file pointed by Envt Var SetEnvCmdFile
................which SETs environment variables
........ECHOes the content of Environment variables 'prepared' by the COBOL program
...................................
...................................
Scenario1:
======
Files used:
CL-SetEnvVarOne.bat ( on Net Express )
or
CL-SetEnvVarOne.sh ( on Server Express )
COBOL program setenvvarone.cbl
The script above
........Sets the Environment variable SetEnvCmdFile
........Compiles setenvvarone.cbl
........Launches setenvvar.int redirecting STDERR to $SetEnvCmdFile
................setenvvarone.int
........................uses DISPLAY aVar UPON SYSERR *> = display redirected to STDERR
................................On Windows: DISPLAY "SET EnvtVarXX= EnvtVarXX_Content"
................................................................upon SYSERR
................................On UNIX
DISPLAY "EnvtVarXX= EnvtVarXX_Content"
................................................................upon SYSERR
....................................................DISPLAY "export EnvtVarXX"
................................................................upon SYSERR
........Launches the scriot file pointed by Envt Var SetEnvCmdFile
................which SETs environment variables
........ECHOes the content of Environment variables 'prepared' by the COBOL program
...................................
...................................
Scenario2:
======
Files used: CL-SetEnvVarTwo.bat ( on Net Express) or CL-SetEnvVarTwo.sh (on Server Express)
COBOL program setenvvartwo.cbl & dosetEnvVar.cbl
The script above
........Sets the Environment variable SetEnvCmdFile
........Compiles setenvvartwo.cbl & dosetEnvVar.cbl
........Launches setenvvartwo.int
................setenvvartwo.int calls the program dosetEnvVar.int to set Environment variable
........................(setenvvartwo just calls a 'STUB' to prepare Environment variables
........................ to be retrieved to the SHELL )
........................dosetEnvVar uses the compiler directive OUTDD($SetEnvCmdFile)
........................I.e. : COBOL display statements are redirected to the content
...................................of the environment variable SetEnvCmdFile
........................The DISPLAY statements are like
........................On Windows:
DISPLAY "set EnvtVarXX=EnvtVarXX_Content"
........................On UNIX
DISPLAY "EnvtVarXX=EnvtVarXX_Content"
.............................................DISPLAY "export EnvtVarXX "
........Launches the scriot file pointed by Envt Var SetEnvCmdFile
..............which SETs environment
variables and display their content