Execute a system command: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
Line 98: Line 98:


This page uses "ls" as the primary example. For what it's worth, Tcl has built-in primitives for retrieving lists of files so one would rarely ever directly exec an ls command.
This page uses "ls" as the primary example. For what it's worth, Tcl has built-in primitives for retrieving lists of files so one would rarely ever directly exec an ls command.

It is also possible to execute a system command by "open"ing it through a pipe from whence any output of the command can be read at any (later) time. For example:

set io [open "|ls" r]

would execute "ls" and pipe the result into the unit "io". From there one could receive it either line by line like this:

set nextline [gets $io]
or read the whole shebang in a fell swoop:

set lsoutput [read $io]


==[[UNIX Shell]]==
==[[UNIX Shell]]==

Revision as of 05:26, 3 February 2007

Task
Execute a system command
You are encouraged to solve this task according to the task description, using any language you may know.

In this task, the goal is to run either the ls system command, or the pause system command.

AppleScript

do shell script "ls" without altering line endings

C

Compiler: GCC 4.0.1

Platform: BSD

#include <stdlib.h>

int main()
{
    system("ls");
}

C++

Compiler: Visual C++ 2005

system("pause");

Haskell

Interpreter: GHCi 6.6

import System.Cmd

main = system "ls"

IDL

 $ls

Will execute "ls" with output to the screen.

 spawn,"ls",result

will execute it and store the result in the string array "result".

 spawn,"ls",unit=unit

will execute it asynchronously and direct any output from it into the LUN "unit" from whence it can be read at any (later) time.

Objective-C

Compiler: GCC 4.0.1 (apple)

NSTask runs an external process with explicit path and arguments.

void runls()
{
    [[NSTask launchedTaskWithLaunchPath:@"/bin/ls"
        arguments:[NSArray array]] waitUntilExit];
}

If you need to run a system command, invoke the shell:

void runSystemCommand(NSString *cmd)
{
    [[NSTask launchedTaskWithLaunchPath:@"/bin/sh"
        arguments:[NSArray arrayWithObjects:@"-c", cmd, nil]]
        waitUntilExit];
}

Or use the C method above.

Perl

Interpreter: Perl

Note the use of grave quotes (or back ticks) instead of "normal" single quotes.

my $results = `ls`;

Back ticks as above returns the results, system as below does not.

system "ls";

Python

Interpreter: Python 2.5

 import os
 code = os.system('ls') # Just execute the command, return a success/fail code
 output = os.popen('ls').read() # If you want to get the output data

or

Interpreter:Python 2.4 (and above)

 import subprocess
 output = subprocess.Popen('ls', shell=True, stdout=subprocess.PIPE).stdout
 print output.read()

Note: The latter is the preferred method for calling external processes, although cumbersome, it gives you finer control over the process.

Tcl

  puts [exec ls]

This page uses "ls" as the primary example. For what it's worth, Tcl has built-in primitives for retrieving lists of files so one would rarely ever directly exec an ls command.

It is also possible to execute a system command by "open"ing it through a pipe from whence any output of the command can be read at any (later) time. For example:

 set io [open "|ls" r]

would execute "ls" and pipe the result into the unit "io". From there one could receive it either line by line like this:

 set nextline [gets $io]

or read the whole shebang in a fell swoop:

 set lsoutput [read $io]

UNIX Shell

UNIX shells are designed to run system commands as a default operation.

ls

If one wants to capture the command's standard output:

CAPTUREDOUTPUT=$(ls)