Re: How to execute shell command and store result to variable?
Posted in 1997
EHLIM wrote: > Can I run a Unix shell command like " ls .... | wc -l " and store the result > into a variable for my program varification purpose. So, if my program > checked that number of files in the directory were less than 3, prompt error > messages, otherwise continue. From a run command, the only thing you can do is return a numeric value, but also see below. > I tried this but it doesn't work:- > > LET run_txt="ls ..... | wc -l" > RUN run_txt RETURNING tot_file > IF tot_file < 3 THEN > ERROR "Incomplete files! Process aborted!" > SLEEP 3 > EXIT PROGRAM > END IF > > The variable tot_file shows value 1 means the shell command has been > sucessfully executed, but not the total number of files in the directory. Note that when you return a value from the shell, a bug (either in the shell or with Informix) causes that value to be multiplied by 256. Thus: If you return You actually get 1 256 2 512 3 768 and so on. So you can test against these values. On an occasion when I needed to return a large value (for example, a checksum value that might be 12345), the value was too large to be returned via the shell. So, a work-around (albeit not glamorous) was to put the value in a temp file and then read from the temp file with a c function linked into the 4ge. (I can provide an example of this). This is also the only method that I know of that you could use a non-numeric return value.