Execute a system command: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added language categories.)
(added AppleScript, C, Haskell, and Objective-C)
Line 2: Line 2:


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

==[[AppleScript]]==
[[Category:AppleScript]]
do shell script "ls" without altering line endings

==[[C]]==
[[Category:C]]
'''Compiler:''' [[GCC]] 4.0.1

'''Platform:''' [[BSD]]
#include <stdlib.h>
int main()
{
system("ls");
}


==[[C plus plus|C++]]==
==[[C plus plus|C++]]==
Line 7: Line 23:
'''Compiler:''' [[Visual C plus plus|Visual C++]] 2005
'''Compiler:''' [[Visual C plus plus|Visual C++]] 2005
system("pause");
system("pause");

==[[Haskell]]==
[[Category:Haskell]]
'''Interpreter:''' [[GHC|GHCi]] 6.6
import System.Cmd
main = system "ls"

==[[Objective-C]]==
[[Category: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]]==
==[[Perl]]==

Revision as of 04:36, 1 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"

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

Note the use of grave quotes instead of "normal" single quotes.

`ls`

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)