簡體   English   中英

如何將stderr和stdout輸出附加到system2 R命令中的文件?

[英]How to append stderr and stdout output to file in system2 R command?

我試圖從R(在Ubuntu上的3.3.1)執行shell命令,如下所示:

system2(command="ls", 
        args=c("-l", "/etc/"), 
        stdout="/tmp/stdout.log", 
        stderr="/tmp/stderr.log", 
        wait=TRUE)

不幸的是,每次執行此操作時都會覆蓋日志文件的內容。 我們可以以某種方式指定這個來執行追加而不是覆蓋嗎?

這按我想要的方式進行......

system(command="ls -l /etc/ >> /tmp/stdout.log 2>> /tmp/stderr.log", 
       wait=TRUE)

而且這個更好,因為它允許使用更現代的系統命令並允許更清晰的記錄。

result <- system2(command="ls", 
                  args=c("-l", "/etc/"), 
                  stdout="/tmp/stdout.log", 
                  stderr="/tmp/stderr.log", 
                  wait=TRUE)
now <- date()
cat(paste0("Executed: ", now, "\n"), file="/tmp/stdoutmain.log", append=TRUE)
file.append("/tmp/stdoutmain.log", "/tmp/stdout.log")
cat(paste0("Executed: ", now, "\n"), file="/tmp/stderrmain.log", append=TRUE)
file.append("/tmp/stderrmain.log", "/tmp/stderr.log")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM