在这个例子中展示用不同语言调用外部命令的方法。觉得这个挺有意思,转来给大家看看,也许某一天你会觉得有用。
这些语言包括
Ada
AppleScript
C
C++
C#
E
Forth
Haskell
IDL
J
Java
Logo
MAXScript
Objective-C
OCaml
Perl
PHP
Pop11
Python
Raven
Ruby
Tcl
Toka
UNIX Shell
原文在 http://www.rosettacode.org/wiki/Execute_a_System_Command
Ada
with Interfaces.C; use Interfaces.C;
procedure Execute_System is
function Sys (Arg : Char_Array) return Integer;
pragma Import(C, Sys, "system");
Ret_Val : Integer;
begin
Ret_Val := Sys(To_C("ls"));
end Execute_System;
AppleScript
do shell script "ls" without altering line endings
C
支持版本 gcc version 4.0.1
平台: BSD
#include <stdlib.h>
int main()
{
system("ls");
}
C++
支持版本: Visual C++ version 2005
system("pause");
C#
支持版本: MCS version 1.2.3.1
using System;
class Execute {
static void Main() {
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents=false;
proc.StartInfo.FileName="ls";
proc.Start();
}
}
E
def ls := makeCommand("ls")
ls("-l")
def [results, _, _] := ls.exec(["-l"])
when (results) -> {
def [exitCode, out, err] := results
print(out)
} catch problem {
print(`failed to execute ls: $problem`)
}
Forth
支持版本: gforth version 0.6.2
s" ls" system
Haskell
支持版本: GHCi version 6.6
import System.Cmd
main = system "ls"
IDL
带屏幕输出的 "ls" :
$ls
将输出保存到数组"result":
spawn,"ls",result
异步执行,将输出转到LUN "unit",以便在以后读取:
spawn,"ls",unit=unit
J
J语言系统命令界面由标准的"task"脚本提供:
load’task’
NB. Execute a command and wait for it to complete
shell ‘dir’
NB. Execute a command but don’t wait for it to complete
fork ‘notepad’
NB. Execute a command and capture its stdout
stdout =: shell ‘dir’
NB. Execute a command, provide it with stdin,
NB. and capture its stdout
stdin =: ‘blahblahblah’
stdout =: stdin spawn ‘grep blah’
Java