RE: How to execute shell command and store result to variable?
Posted in 1997
The C code example talked about here is fine but I try not to use C
unless I really have to. Why not have your shell write to a file and
then in 4GL load that file into a table and then deal with the output
that way......
EX:
create temp table t_table (c1 char(1000));let l_file = "/....."
run "ls ... |wc -l > ",l_file clipped
load from l_file insert into t_tablerun "rm ",l_file clipped
Now you can deal with the output of you shell from the table.....
Russ....
}----------
}From: Jacob Salomon[SMTP:jake@apparel.net]
}Sent: Friday, March 21, 1997 4:03 AM
}To: informix-list@rmy.emory.edu
}Subject: Re: How to execute shell command and store result to variable?
}
}EHLIM wrote:
}>
}> Hi Informix programmers,
}>
}> 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.
}>
}> 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.
}
}Ehlim,
}
}I have no knowlege of any 4GL way to redirect the stdout of a program to
}a program variable. In my experience, I have seen such a capability in
}the various shells, AWK, and PERL.. so your request is a rather tall
}order. However, you can still get it if you are willing to break into C
}and do what the shell does:
} - open a pipe file
} - fork a child process
} - have the child proces redirect its stdout to the pipe
} - have the child process EXEC the program you want to run
} - Let the parent process capture the stdout of the child process
} - call pushquote and whatever else is needed to return a value to the
}calling
} 4GL routine
} - Smash your fist through the screen when it takes a week to debug this
}code. ;)
}
}well, there's probably a library function to do all the dirty work
}above; all you need to do is get the 4GL<->call/return conventions
}straight.
}
}For some situations, it might pay to have a special database table with
}many columns to serve as a communication area between programs. Of
}course, both programs would have to be database apps. This easy way out
}does not apply to your intention of running ordinary shell commands.
}
}Good luck. And if you find a UNIX library function to do that piping,
}spawning, and stdout-capture, please post a reply with the name of that
}function.
}--
} -- Jake (Lost in thought and won't ask for directions)
} . .
} _..-'( )`-.._
} ./'. '||\\\\. }\\_/{ .//||` .`\\.
} ./'.|'.'||||\\\\|.. )o o( ..|//||||`.`|.`\\.
} ./'..|'.|| |||||\\`````` \\,@,/ ''''''/||||| ||.`|..`\\.
} ./'.||'.|||| ||||||||||||. ||| .|||||||||||| ||||.`||.`\\.
} /'|||'.|||||| ||||||||||||{ | }|||||||||||| ||||||.`|||`\\
} '.|||'.||||||| ||||||||||||{ | }|||||||||||| |||||||.`|||.`
}'.||| ||||||||| |/' ``\\||`` | ''||/'' `\\| ||||||||| |||.`
}|/' \\./' `\\./ \\!|\\ /|!/ \\./' `\\./ `\\|
}V V V }' `\\ /' `{ V V V
}\\ \\ \\ V / / /
}+-----------------------------------------------------------+
}| Impeccable Logic: A thought process which successfully |
}| resists chicken bites |
}+-----------------------------------------------------------+
}