system()

阅读(1995) 标签: 系统命令, 执行,

描述:

调用系统命令,执行完毕后返回。

语法:

system(cmd)

备注:

调用并执行cmdshpowershell等类型的系统命令。

该函数目前只支持执行一条命令,如果想执行多条命令,可以把多条命令写入batsh等可执行文件中,然后通过命令调用文件。

参数:

cmd

系统命令,支持cmdshpowershell等类型的系统命令。

参数形式固定为“cmd /c +命令”、 powershell -c + 命令”、“sh -c +命令”…

选项:

@p

不等待,继续执行。

@o

返回stdout的结果。

@a

返回成[exit,stdout,stderr]的序列。

返回值:

相应的执行结果

示例:

system("cmd /c ipconfig")

获取当前机器的网络相关信息。

system("cmd /c D:\\MicroInsight\\esCalc\\bin\\startup-zh.bat")

执行startup-zh.bat

system("cmd /c D:\\MicroInsight\\DataLogic\\bin\\DataLogic.exe")

执行DataLogic.exe

system("sh -c /test.sh")

执行test.sh

system("powershell -c copy D:\\stu.txt D:\\file\\stu.txt")

将文件stu.txtD:复制到D:\\file目录下。

  使用@o选项:

system@o("sh -c ifconfig")

使用@o选项,返回stdout的结果:

  使用@a选项: 

system@a("powershell /c Remove-Item D:\\test.txt")

使用@a选项,返回成[exit,stdout,stderr]的序列,执行命令报错时:

system@a("powershell /c New-Item E:\\t1.txt")

使用@a选项,正确执行命令时: