有没有办法让R运行脚本并在错误时将非0返回给shell?
发布时间:2020-12-15 17:02:47 所属栏目:安全 来源:网络整理
导读:给定一个包含3行的文件script.R: print('hello')stop('user error')print('world') 我们可以运行它: $R -f script.R print('hello')[1] "hello" stop('user error')Error: user error print('world')[1] "world" 但在错误发生后仍然如此.我希望它在出错时
给定一个包含3行的文件script.R:
print('hello') stop('user error') print('world') 我们可以运行它: $R -f script.R > print('hello') [1] "hello" > stop('user error') Error: user error > print('world') [1] "world" 但在错误发生后仍然如此.我希望它在出错时暂停脚本.这样做: $R -e "source('script.R')" > source('script.R') [1] "hello" Error in eval(expr,envir,enclos) : user error Calls: source -> withVisible -> eval -> eval 好.它停止在错误上运行脚本.但是shell的返回值是0(成功): $echo $? 0 反正有没有运行脚本,停止错误,并返回非零到shell?我搜查了一下,找不到答案. 我可以grep输出文件中的文本“错误”,但这有一些需要管理的风险;例如以某种方式greping错误的输出文件并且两次或多次运行写入同一文件.可以管理这些问题,但是将非零返回到shell会更简单,更健壮.在脚本末尾添加一行也是一种解决方法,因为我需要将该行添加到所有脚本中.
感谢@Pascal的评论,结果发现在我的.Rprofile中我有:
options(error=quote(dump.frames())) 当我使用–vanilla运行以防止我的.Rprofile被加载: $R --vanilla -f script.R > print('hello') [1] "hello" > stop('user error') Error: user error Execution halted $echo $? 1 这正是我想要的并解决问题.谢谢@Pascal! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |