【发布时间】:2019-09-11 08:05:56
【问题描述】:
我有一系列在 powershell 中调用的 R 文件。如果出现错误,我不希望它继续尝试运行后续的 R 脚本。我遇到的问题是在 powershell 中捕获 R 错误,并使用它来终止脚本。有任何想法吗?
到目前为止,我已经尝试过了,但没有发现错误。
Powershell 脚本:
# loop some scripts, try running them and if they fail then stop the process
foreach ($element in @(1,2,3)) {
Write-Host $element
try{
# minimal working eg of a script that stops early
Rscript -e "stop('error')"
}
catch {
Write-Host 'Rscript failed on run' $element'. Process killed in 10 seconds'
Start-Sleep 10
Exit
}
}
由于它没有捕获错误,因此输出消息是:
1
Error: error
Execution halted
2
Error: error
Execution halted
3
Error: error
Execution halted
任何建议都会很棒!谢谢
编辑:
根据评论,我现在正在使用这个(供将来参考):
foreach ($element in @(1,2,3)) {
Write-Host $element
# minimal working eg of a script that stops early
Rscript -e "stop('error')"
if ($LastExitCode -ne 0){
Write-Host 'Rscript failed on run' $element'. Process killed in 10 seconds'
Start-Sleep 10
Exit
}
}
【问题讨论】:
-
啊,谢谢,这太完美了。我在谷歌中找不到合适的词来得到这样的答案,我在 :p.如果你愿意,你可以添加这个作为答案,我会接受它
标签: r powershell