R语言shell、system、shell.exec函数详解

一、system函数:调用参数command指定的操作系统命令

system(command,intern=false,ignore.stdout=false,ignore.stderr=false,wait=true,input=NULL,show.output.on.console=true,minimized=false,invisible=true)
1.command:字符串
    解析为一个命令+空格分隔的参数,如果命令的路径(或单个参数本身-如文件路径)包括空格,则必须由shQuote引用,在Windows上只允许使用双引号(可以使用paste,paste0拼接),windows路径名上不允许包含双引号,所以不用担心转义嵌入的引号
    command必须是可执行文件(扩展名为.exe,.com);批处理文件(扩展名.bat,.cmd),如果没有提供将依次尝试这些扩展名,不能使用重定向\DOS内部命令\管道
    command可以是shell认为可执行的任何内容,包括shell脚本,也可以包括多个命令,命令以;区分开,在Windows系统中函数system不会使用shell,但shell()函数可将命令传递给shell
2.intern:声明是否将系统命令的执行结果作为R的字符串输出(true则用popen调用命令,否则用C函数system调用)
3.wait:R是否需要等待操作系统命令执行完毕,或者是可以异步执行;如果设置intern=T,则该参数将被忽略
4.show.output.on.console:声明是否将执行操作系统命令的结果,显示在R控制台前
5.minimized:是否将CMD窗口最小化表示
    注:最后两个参数只能在Windows操作系统中使用
    注:如果命令无法运行,status返回值127
    注:如果得到一个非零的退出状态,则抛出一个警告信息,并在结果属性status中提示
6.可通过键盘操作(Rgui中使用Esc键,Rterm中使用Ctrl+C)或在Rgui菜单里,中断正在运行(等待中)的命令:这个操作会将控制权返回给R控制台.R会试图完全中断该过程,但可能需要强制终止,并有可能丢失未保存的工作等

#使用-f列出当前目录中的所有文件 
system("ls -f")
#t1是一个字符向量,各个元素分别对应于who输出的每一行(假设该平台存在对象who) 
t1<-try(system("who", intern = TRUE))
system(paste('"c:/Program Files/Mozilla Firefox/firefox.exe"', '-url cran.r-project.org'), wait = FALSE)
system("java -jar F:/.../SeleniumSever/selenium-server-standalone-3.8.1.jar",wait = F)
system("java -Dwebdriver.chrome.driver=F:/.../SeleniumSever/chromedriver.exe",wait = F)

二、shell函数简介:使用shell运行cmd在shell下的命令

shell(cmd,shell,flag=”\c”,intern=FALSE,wait=TRUE,translate=FALSE,mustWork=FALSE)
1.translate:如果TRUE,“/”在cmd中将被转换为“\”
2.mustWork:如果TRUE,运行命令失败会提示R错误,FALSE则出现警告信息
3.shell:shell名称字符串(bash,tcsh,sh),使用shell=NULL则调用cmd命令,在这种情况下.exe是假定的扩展名,如果给出扩展名,可以直接使用批处理文件
shell是对system更友好化的包装(升级版、补充版,shell在原CMD基础上做了很多修改调整,增加了很多命令及命令别名),该功能仅在Windows上存在

三、shell.exec函数:打开一个文件或URL

#注意设置默认浏览器
shell.exec("http://fund.eastmoney.com/favor.html")
shell.exec("C:/Program Files/BreezeSys/BreezeBrowser/Breezebrowser.htm")



  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Spring Boot 中执行 shell 命令,可以使用 Java 的 `Runtime` 类或者使用 Apache Commons Exec 库。 1. 使用 `Runtime` 类: ```java import java.io.BufferedReader; import java.io.InputStreamReader; public class ShellCommandExecutor { public static void main(String[] args) { try { String command = "ls -l"; // 要执行的 shell 命令 Process process = Runtime.getRuntime().exec(command); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } int exitCode = process.waitFor(); System.out.println("Exited with error code : " + exitCode); } catch (Exception e) { e.printStackTrace(); } } } ``` 2. 使用 Apache Commons Exec 库,需要将其添加到 Maven 依赖中: ```xml <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-exec</artifactId> <version>1.3</version> </dependency> ``` 然后可以使用以下代码执行 shell 命令: ```java import org.apache.commons.exec.CommandLine; import org.apache.commons.exec.DefaultExecutor; import org.apache.commons.exec.ExecuteException; import org.apache.commons.exec.ExecuteResultHandler; import org.apache.commons.exec.ExecuteStreamHandler; import org.apache.commons.exec.Executor; import org.apache.commons.exec.PumpStreamHandler; import java.io.ByteArrayOutputStream; import java.io.IOException; public class ShellCommandExecutor { public static void main(String[] args) { try { String command = "ls -l"; // 要执行的 shell 命令 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream errorStream = new ByteArrayOutputStream(); CommandLine commandLine = CommandLine.parse(command); ExecuteStreamHandler streamHandler = new PumpStreamHandler(outputStream, errorStream); Executor executor = new DefaultExecutor(); executor.setStreamHandler(streamHandler); // 可以使用 ExecuteResultHandler 进行异步执行 //executor.execute(commandLine, new MyExecuteResultHandler()); int exitCode = executor.execute(commandLine); String output = outputStream.toString(); String error = errorStream.toString(); System.out.println("Output:\n" + output); System.out.println("Error:\n" + error); System.out.println("Exited with error code : " + exitCode); } catch (ExecuteException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } // 异步执行的示例 ExecuteResultHandler class MyExecuteResultHandler implements ExecuteResultHandler { @Override public void onProcessComplete(int exitValue) { System.out.println("Command executed successfully."); } @Override public void onProcessFailed(ExecuteException e) { System.err.println("Command execution failed."); e.printStackTrace(); } } ``` 这样你就可以在 Spring Boot 中执行 shell 命令了。请注意,执行 shell 命令具有一定的安全风险,请谨慎使用,并确保你信任要执行的命令。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值