gpt4 book ai didi

shell - 使用 shell() 执行多个 shell 命令的推荐方法

转载 作者:行者123 更新时间:2023-12-04 01:33:17 27 4
gpt4 key购买 nike

在snakemake中,使用shell()函数执行多个命令的推荐方式是什么?

最佳答案

您可以调用shell()多次内run规则块(规则可以指定 run: 而不是 shell: ):

rule processing_step:
input:
# [...]
output:
# [...]
run:
shell("somecommand {input} > tempfile")
shell("othercommand tempfile {output}")

否则,由于 run 块接受 Python 代码,您可以将命令列表构建为字符串并遍历它们:

rule processing_step:
input:
# [...]
output:
# [...]
run:
commands = [
"somecommand {input} > tempfile",
"othercommand tempfile {output}"
]
for c in commands:
shell(c)

如果在执行规则期间不需要 Python 代码,则可以在 shell 中使用三引号字符串。块,然后像在 shell 脚本中一样编写命令。对于纯 shell 规则来说,这可以说是最易读的:

rule processing_step:
input:
# [...]
output:
# [...]
shell:
"""
somecommand {input} > tempfile
othercommand tempfile {output}
"""

如果 shell 命令依赖于前面命令的成功/失败,它们可以与通常的 shell 脚本操作符一起使用,如 ||&& :

rule processing_step:
input:
# [...]
output:
# [...]
shell:
"command_one && echo 'command_one worked' || echo 'command_one failed'"

关于shell - 使用 shell() 执行多个 shell 命令的推荐方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45553762/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com