- 浏览: 1944878 次
- 性别:
- 来自: 南京
文章分类
最新评论
-
cht的大摩托:
学习
IBM WebSphere Performance Tool / ISA / jca457.jar / ha456.jar / ga439.jar -
leeking888:
有没有linux 64位的相关librfccm.so等包啊?
web test LoadRunner SAP / java / Java Vuser / web_set_max_html_param_len -
paladin1988:
非常不错,多谢了。。
appServer IBM WebSphere / WAS 7 / 8.5 / was commerce -
hzxlb910:
写了这么多
net TCP/IP / TIME_WAIT / tcpip / iperf / cain -
acwyg:
ed2k://|file|LoadRunner.V8.1.is ...
web test performance tools / linux performance tools / windows performance tools
http://www.cocoachina.com/b/?p=171 http://rosettacode.org/wiki/Execute_a_System_Command
In this task, the goal is to run either the ls (dir on Windows) system command, or the pause system command.
在这个例子中展示用不同语言调用外部命令的方法。觉得这个挺有意思,转来给大家看看,也许某一天你会觉得有用。 这些语言包括 Ada 原文在 http://www.rosettacode.org/wiki/Execute_a_System_Command 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; do shell script "ls" without altering line endings 支持版本 gcc version 4.0.1 平台: BSD #include <stdlib.h> int main() { system("ls"); } 支持版本: Visual C++ version 2005 system("pause"); 支持版本: 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(); } } 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`) } 支持版本: gforth version 0.6.2 s" ls" system 支持版本: GHCi version 6.6 import System.Cmd main = system "ls" 带屏幕输出的 "ls" : $ls 将输出保存到数组"result": spawn,"ls",result 异步执行,将输出转到LUN "unit",以便在以后读取: spawn,"ls",unit=unit 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 version 1.4+ 有两种执行系统命令的方法,简单的方法会挂起JVM import java.io.IOException; import java.io.InputStream; public class MainEntry { public static void main(String[] args) { executeCmd("ls -oa"); } private static void executeCmd(String string) { InputStream pipedOut = null; try { Process aProcess = Runtime.getRuntime().exec(string); aProcess.waitFor(); pipedOut = aProcess.getInputStream(); byte buffer[] = new byte[2048]; int read = pipedOut.read(buffer); // Replace following code with your intends processing tools while(read >= 0) { System.out.write(buffer, 0, read); read = pipedOut.read(buffer); } } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException ie) { ie.printStackTrace(); } finally { if(pipedOut != null) { try { pipedOut.close(); } catch (IOException e) { } } } } } 正确的方法使用进程提供的线程去读取InputStream。 import java.io.IOException; import java.io.InputStream; public class MainEntry { public static void main(String[] args) { // the command to execute executeCmd("ls -oa"); } private static void executeCmd(String string) { InputStream pipedOut = null; try { Process aProcess = Runtime.getRuntime().exec(string); // These two thread shall stop by themself when the process end Thread pipeThread = new Thread(new StreamGobber(aProcess.getInputStream())); Thread errorThread = new Thread(new StreamGobber(aProcess.getErrorStream())); pipeThread.start(); errorThread.start(); aProcess.waitFor(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException ie) { ie.printStackTrace(); } } } class StreamGobber implements Runnable { private InputStream Pipe; public StreamGobber(InputStream pipe) { if(pipe == null) { throw new NullPointerException("bad pipe"); } Pipe = pipe; } public void run() { try { byte buffer[] = new byte[2048]; int read = Pipe.read(buffer); while(read >= 0) { System.out.write(buffer, 0, read); read = Pipe.read(buffer); } } catch (IOException e) { e.printStackTrace(); } finally { if(Pipe != null) { try { Pipe.close(); } catch (IOException e) { } } } } } 支持版本: UCB Logo SHELL命令返回列表: print first butfirst shell [ls -a] ; .. dosCommand "pause" 支持版本:苹果公司的GCC version 4.0.1 void runls() { [[NSTask launchedTaskWithLaunchPath:@"/bin/ls" arguments:[NSArray array]] waitUntilExit]; } 如果你希望调用系统命令,先执行shell: void runSystemCommand(NSString *cmd) { [[NSTask launchedTaskWithLaunchPath:@"/bin/sh" arguments:[NSArray arrayWithObjects:@"-c", cmd, nil]] waitUntilExit]; } 同样可以使用上面的C语言调用方法。 Sys.command "ls" my @results = qx(ls); # runs command and returns its STDOUT my @results = `ls`; # dito, alternative syntax system "ls"; # runs command and returns its exit status print `ls`; #The same, but with back quotes exec "ls"; # replace current process with another 另外可以参阅 http://perldoc.perl.org/perlipc.html#Using-open()-for-IPC http://perldoc.perl.org/IPC/Open3.html 首行执行命令,第二行显示输出: @exec($command,$output); echo nl2br($output); 注意这里的‘@’防止错误消息的显示,‘nl2br’ 将 ‘\n’转换为HTML的‘br’ sysobey(’ls’); 支持版本: Python version 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 或者 支持版本: Python version 2.4 (及以上版本) import subprocess output = subprocess.Popen(’ls’, shell=True, stdout=subprocess.PIPE).stdout print output.read() 后者是比较好的方法。 或者 支持版本: Python version 2.2 (及以上版本) import commands stat, out = commands.getstatusoutput(’ls’) if not stat: print out `ls -la` as listing 或者指定任何字符串 ‘ls -la’ shell as listing string = `ls` puts [exec ls] 同样可以使用系统open命令。 set io [open "|ls" r] 获取结果的方法是 set nextline [gets $io] 或者 set lsoutput [read $io] 如果命令是以RW方式打开,可以用同样的方法发送用户的输入。 needs shell " ls" system 直接调用 ls 如果希望获取标准输出 CAPTUREDOUTPUT=$(ls) 在 C-Shell 中可以这样做 set MYCMDOUTPUT = `ls` echo $MYCMDOUTPUT 在Korn Shell 中是这样: MYCMDOUTPUT=`ls` echo $MYCMDOUTPUT24种语言执行外部命令的方法
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 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
end
发表评论
-
script Ngrinder_TestRunnerInsertMysqlSingle.groovy
2017-06-21 10:49 1029s 阿里巴巴Java开发规范手册.zip http:/ ... -
script javascript / Node.js / nodejs / GraphicsMagick / sails
2016-06-02 16:32 716s Node.js http://baike.baid ... -
script php / php-fpm /
2015-02-03 18:16 866s Loadrunner project 006 ... -
script perl / pcre / Perl Compatible Regular Expressions
2013-01-31 13:05 2298正则表达式库 PCRE 8.32 http://ww ... -
script asp / asp error_list / Active Server Page
2011-12-19 00:53 6572ASP / Active Server Page h ... -
script webshell jspWebShell / pythonWebShell / phpWebShell
2011-06-11 11:01 2417google shell http://www.jiun ... -
script js base
2009-10-25 16:23 1473三种不同位置的JavaScrip t 代码的写法 ... -
js calendar / wannianli
2009-09-21 13:41 2601s 二十四节气表 http://baike.b ... -
js statistic / URL code
2009-09-18 21:22 2583s java net unicode / nati ... -
script openLaszlo / openLaszlo Server
2009-09-04 15:17 1925http://www.iteye.com/news/2 ... -
js tools / framework / firebug
2009-02-23 16:03 1724http://imgur. ... -
js Colletion
2009-02-06 15:21 1824调用外部js获取IP地址的方法一 http:// ... -
script python / TurboGears / Django / Pylons / ZOPE
2008-10-25 10:10 3244获取python的版本号 http://hi.baidu ... -
script php / php bbs/ php blog / php cms / php cmf / vhcs2
2008-10-20 12:29 4529php docs http://www.cyberci ... -
script ActionScript / ColdFusion
2008-09-26 13:34 1653Sothink SWF Decompiler V4.5 汉化 ... -
script Groovy / Grails
2008-09-26 01:49 2063s Groovy Groov ... -
script js jquery / ext / aptana / json / dojo / dwr / yui / ligerui
2008-09-24 09:41 3447json JSON与JAVA数据的转换 http: ... -
script VBScript
2008-09-22 01:13 17412VBScript 教程 http://www.w3c ... -
js Connection
2008-09-12 17:13 2723http://guoqinhua1986-126-com.it ... -
script Ruby / Rails / Arachni
2008-09-04 22:26 1805ruby down http://rubyinstal ...
相关推荐
`system`函数是C标准库中的一个函数,它允许程序执行DOS命令或者Windows的命令解释器(cmd.exe)来运行其他程序。它会启动一个新的命令解释器实例并执行指定的命令字符串。 (三)源码示例: ```cpp #include int ...
在`Main`方法中,你可以调用`ExecuteCommand`方法,将你想要执行的cmd命令作为参数传入,并将输出打印到控制台: ```csharp static void Main(string[] args) { string commandOutput = ExecuteCommand("dir")...
当我们想要执行一个特定的CMD命令,例如`dir`或`ipconfig`,只需将命令作为字符串传递给`ExecuteCommand`函数即可。例如: ```csharp string result = ExecuteCommand("ipconfig /all"); Console.WriteLine(result)...
public string ExecuteCommand(string command) { // 创建ProcessStartInfo对象,设置命令行参数 ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = "cmd.exe"; // 指定要启动的程序 psi.Redirect...
public void ExecuteCommand(string command) { ProcessStartInfo psi = new ProcessStartInfo("cmd.exe", "/c " + command); psi.CreateNoWindow = true; // 隐藏CMD窗口 psi.UseShellExecute = false; // 不...
2. **具体命令(Concrete Command)**:实现了`Command`接口,持有一个指向接收者的引用,并实现`execute()`方法。这个方法通常会调用接收者的某些方法来执行特定操作。例如,如果你正在构建一个多媒体播放器,可以...
Command cmd = new Command(); // 设置连接字符串 string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\myDatabase.mdb;User Id=admin;Password=;"; // 打开连接 conn.Open...
本文实例讲述了C#执行外部命令的方法。分享给大家供大家参考。具体实现方法如下: /// ///executes a system command from inside csharp ///</summary> ...public static int executeCommand(string cmd, in
这里,`i:Interaction.Triggers`和`i:EventTrigger`来自`System.Windows.Interactivity`命名空间,`cmd:InvokeCommandAction`则需要引用`Microsoft.Xaml.Behaviors.Wpf`库(或者使用开源的`Prism`库)。当TextBox...
public static int ExecuteSql(string SQLString) { using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand(SQLString, connection)) { try {...
在实际编程中,`CmdRomate` 类可能会有一个方法(如 `executeCommand`),接收一个字符串参数(如 "shutdown /s /t 60"),然后使用 `system` 函数或者更安全的 `CreateProcess` API 来执行这个命令。`CmdRomate.h` ...
Proc.CommandLine := 'cmd /c ' + Command; Proc.Options := [poUseCurrentDir, poWaitOnExit]; Proc.Execute; finally Proc.Free; end; end; ``` `TProcess`的`Options`属性可以控制如何执行命令,如等待...
public native int executeCommand(String command); } ``` 在对应的C/C++文件(例如`ffmpeg.cpp`)中,实现`executeCommand`函数: ```c++ extern "C" JNIEXPORT jint JNICALL Java_...
使用Command对象执行SQL查询,创建一个Command对象,设置其CommandText属性为SQL语句,然后调用Execute方法。例如,执行一个简单的SELECT语句: ```vb Dim cmd As New ADODB.Command cmd.ActiveConnection = conn ...
cmd.Execute(out dr); ``` 接着,通过循环遍历`ADODataReader`的每一行,读取并显示数据: ```csharp while (dr.Read()) { System.Console.WriteLine(dr["FirstName"]); } ``` 最后,关闭连接以释放资源: ```...
private void ExecuteCommand(OleDbCommand cmd) { cmd.Connection=m_Connection; try { m_Connection.Open(); cmd.ExecuteNonQuery(); } finally { m_Connection.Close(); } } public DataSet ...
executeShellCommand(cmd); } } ``` 这里,`callShellCommand`方法会调用`executeShellCommand`,后者是通过JNI调用的本地方法。 至于`logSystem.zip`,这可能包含了一个用于记录和查看系统日志的工具或示例。在...
MySQLCommand cmd = new MySQLCommand(sql, conn); // 执行非查询命令 int number = cmd.ExecuteNonQuery(); // 设置字符集 MySQLCommand commn = new MySQLCommand("set names gb2312", conn); commn....
`os.system`函数则被用来在CMD环境中执行MySQL命令,如查看所有数据库或执行更复杂的脚本。 在实际应用中,你可以根据需要调整`run_mysql_command`函数中的参数,比如执行更复杂的DML(数据操纵语言)或DDL(数据...