Execute a system command: Difference between revisions

→‎{{header|Clojure}}: 15th CMake example: it also does pipelines!
(→‎{{header|Clojure}}: 15th CMake example: it also does pipelines!)
Line 146:
<lang lisp>(.. Runtime getRuntime (exec "cmd /C dir"))</lang>
 
=={{header|CMake}}==
{{works with|Unix}}
<lang cmake>execute_process(COMMAND ls)</lang>
 
Because of a quirk in the implementation ([http://cmake.org/gitweb?p=cmake.git;a=blob;f=Source/cmExecuteProcessCommand.cxx;hb=HEAD cmExecuteProcessCommand.cxx] and [http://cmake.org/gitweb?p=cmake.git;a=blob;f=Source/kwsys/ProcessUNIX.c;hb=HEAD ProcessUNIX.c]), CMake diverts the standard output to a pipe. The effect is like running <code>ls | cat</code> in the shell. The ''ls'' process inherits the original standard input and standard error, but receives a new pipe for standard output. CMake then reads this pipe and copies all data to the original standard output.
 
''execute_process()'' can also chain commands in a pipeline, and capture output.
 
<lang cmake># Calculate to 40 digits after the decimal point.
execute_process(
COMMAND printf "scale = 45; 4 * a(1) + 5 / 10 ^ 41\\n"
COMMAND bc -l
COMMAND sed -e "s/.\\{5\\}$//"
OUTPUT_VARIABLE pi OUTPUT_STRIP_TRAILING_WHITESPACE)
message(STATUS "pi is ${pi}")</lang>
 
<pre>-- pi is 3.1415926535897932384626433832795028841972</pre>
 
=={{header|Common Lisp}}==
Anonymous user