Execute a system command: Difference between revisions

Content added Content deleted
(→‎{{header|Objective-C}}: added complete example (because of NSAutoreleasePool I believe it is needed))
No edit summary
Line 1: Line 1:
{{Task|Programming environment operations}}
{{Task|Programming environment operations}}


In this task, the goal is to run either the <code>ls</code> (<code>dir</code> on Windows) system command, or the <code>pause</code> system command.
In this task, the goal is to run either the <tt>ls</tt> (<tt>dir</tt> on Windows) system command, or the <tt>pause</tt> system command.


=={{header|Ada}}==
=={{header|Ada}}==
Line 112: Line 112:
=={{header|Java}}==
=={{header|Java}}==
{{works with|Java|1.5+}}
{{works with|Java|1.5+}}
<java>import java.util.Scanner;
<lang java>import java.util.Scanner;
import java.io.*;
import java.io.*;


Line 126: Line 126:
}
}
}
}
}</java>
}</lang>


{{works with|Java|1.4+}}
{{works with|Java|1.4+}}
Line 281: Line 281:
<!-- {{libheader|Cocoa}} -->
<!-- {{libheader|Cocoa}} -->


<objc>#import <Cocoa/Cocoa.h>
<lang objc>#import <Cocoa/Cocoa.h>
//works also with
//works also with
//#import <Foundation/Foundation.h>
//#import <Foundation/Foundation.h>
Line 302: Line 302:
[pool release];
[pool release];
return 0;
return 0;
}</objc>
}</lang>




Line 310: Line 310:
Just run the command:
Just run the command:


<ocaml>Sys.command "ls"</ocaml>
<lang ocaml>Sys.command "ls"</lang>


To capture the output of the command:
To capture the output of the command:


<ocaml>#load "unix.cma";;
<lang ocaml>#load "unix.cma";;
let syscall cmd =
let syscall cmd =
let inc, outc = Unix.open_process cmd in
let inc, outc = Unix.open_process cmd in
Line 325: Line 325:
let _status = Unix.close_process (inc, outc) in
let _status = Unix.close_process (inc, outc) in
Buffer.contents buf;;
Buffer.contents buf;;
let listing = syscall "ls";;</ocaml>
let listing = syscall "ls";;</lang>


=={{header|Perl}}==
=={{header|Perl}}==