24种语言执行外部命令的方法
时间: 2009-06-11 19:44 点击: 次
在这个例子中展示用不同语言调用外部命令的方法。觉得这个挺有意思,转来给大家看看,也许某一天你会觉得有用。
这些语言包括
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
顶一下(3)
100%
踩一下(0)
0%
发表评论
本类文章点击排行榜
- [06-18] 苹果提供SDK 3.0 Final正式下载
- [04-19] App Store中国区突然波动,大多
- [06-11] 软件开发者从魔兽世界中需要
- [05-17] 5月Tiobe编程语言排行榜:Obj
- [02-05] 苹果要求开发者同意新的开发
- [04-02] App Store开放iPad专区,下载前三
- [07-02] iAd 广告平台正式上线了
- [06-11] 24种语言执行外部命令的方法
- [04-09] 苹果公布iPhone SDK 4 beta,附下
推荐内容
最近更新
- [08-03] 2010年7月编程语言排行榜,Objective-C 依然第九
- [08-02] 开发者也可以购买 iAd 广告位推广自己的 App 了
- [07-30] AppleID用户名密码中的特殊字符会阻碍App Loader上传
- [07-28] 苹果公布 iOS 4.1 Beta 2
- [07-23] iPhone 用户的手机系统版本分布统计
- [07-23] 苹果开放 Xcode 4 Preview 2 下载
- [07-21] 7月22号起,开发者只能用 Application Loader 上传应用
- [07-15] 苹果向开发者开放 iOS 4.1 SDK Beta 1
- [07-14] 苹果首位核心高管开通Twitter引发关注


5月Tiobe编程语言排行榜:Objective-C升入前十名
苹果将于6月7-11日举办WWDC 2010开发者大会
Apple在下一盘很大的棋:评Apple收购Siri
4月16日晚8点,北京CocoaChina会员聚会通知
苹果将于4月8日发布iPhone OS 4.0
苹果要求开发者同意新的开发者协议