We’re running Natural / ADABAS under UNIX AIX 188.8.131.52.
I was wondering how could I gracefully verify that a given AIX path followed by a file name can actually be validated from inside my Natural program, before issuing a READ WORK FILE #… command that would result in a “hard” abend with the message:
"NAT1599 Attempt to execute READ/WRITE WORK to non-existent file. " …if the file didn’t exist!
I know how to verify the existence of a file from the AIX prompt, or from inside of a bash script… so I guess the question becomes: how do I issue native AIX commands and have access to their return codes, from inside of a Natural program?
e.g.: I can issue the following in a bsh/ksh script
if [ ! -f $MySampleFileName ];
echo “File $MySampleFileName does not exist”
…and get an answer as to whether or not my MySampleFileName (which was passed from outside into the script executing the code snippet above), contains a string consisting of path/filename that results in a real file name, out there, in the UNIX file system. I’m looking for a way of accomplishing this from inside of a Natural program.
Thanks so much!
USR1052N in library SYSEXT will let you send an arbitrary command to
the operating system.
Hth, best regards,
I recently created the following function for our environment (HP-UX 11i v3/NAT 6.3.11). It appears to be working as expected.
DEFINE FUNCTION F#FILEEXISTS
DEFINE DATA PARAMETER
1 #FILE (A) DYNAMIC BY VALUE /* File path and name
1 #CMD (A) DYNAMIC
COMPRESS '[ -f' #FILE ']' INTO #CMD
CALL 'SHCMD' #CMD 'NOSCREENIO'
IF RET('SHCMD') = 0
F#FILEEXISTS := TRUE
F#FILEEXISTS := FALSE
The function is called as shown.
The path can also be hardcoded if needed.
I went with SHCMD over USR1052N because of the A253 input limit on the user exit.
USR1052N wraps SHCMD, while there’s nothing wrong with using it directly
for the time being it’s always safer to use the interfaces provided in SYSEXT
to shield from possible future changes to the “lower level” functions.
Re. the A253 limit, I think USR1052N has been there before dynamic alpha
variables were introduced into Natural, it might be worthwhile to ask for
a new USR routine with a dynamic alpha parameter.
Here’s a pure natural solution:
* Subprogram FILEXIST
define data parameter
01 #in-filename (A) dynamic by value
01 #out-result (N7) by value result
01 #a1 (A1)
#out-result := *ERROR-NR
define work file 30 #in-filename
read work file 30 once #a1
close work file 30
define data local
01 #rc (N7)
callnat 'FILEXIST' '/home/myfile.txt' #rc
write '=' #rc
decide on first value of #rc
value 1599 write 'does not exist'
value 0 write 'does exist and is readable'
none value write 'some other error'