對於shell
命令,你總是可以利用的條件「或」 ||
:
rule some_rule:
output:
"outfile"
shell:
"""
command_that_errors || true
"""
# or...
rule some_rule:
output:
"outfile"
run:
shell("command_that_errors || true")
通常的退出代碼零(0)表示成功,任何非零指示失敗。包括|| true
可確保在命令以非零退出代碼退出時成功退出(true
始終返回0
)。
如果您需要允許特定的非零退出代碼,則可以使用shell或Python來檢查代碼。對於Python來說,它將如下所示。使用shlex.split()
模塊,因此shell命令不需要作爲參數數組傳遞。
import shlex
rule some_rule:
output:
"outfile"
run:
try:
proc_output = subprocess.check_output(shlex.split("command_that_errors {output}"), shell=True)
# an exception is raised by check_output() for non-zero exit codes (usually returned to indicate failure)
except subprocess.CalledProcessError as exc:
if exc.returncode == 2: # 2 is an allowed exit code
# this exit code is OK
pass
else:
# for all others, re-raise the exception
raise
在shell腳本:
rule some_rule:
output:
"outfile"
run:
shell("command_that_errors {output} || rc=$?; if [[ $rc == 2 ]]; then exit 0; else exit $?; fi")