python - 使用多个命令运行 for 循环文件,当第一个命令前一个文件完成时运行下一个文件

标签 python bash shell

对于一个项目,我创建了多个 python 脚本,我想在 shell 脚本中的文件目录上运行这些脚本。在此 shell 脚本中,我已经创建了一个包含多个命令的 for 循环。 第一个命令是一个 python 脚本,它将输入文件与本地数据库进行对比并占用大部分核心。 接下来的命令占用更少的核心,但需要很多时间。 对于每个文件来说,一系列命令运行非常重要。 为了节省时间,我想更改 shell 脚本以运行文件的第一个命令,并在完成后同时运行输出上的下一个命令和下一个文件上的第一个命令。

有人可以帮我解决这个问题吗?我试图 self 寻找,但找不到答案。我还没有尝试运行这个脚本,因为我已经在没有 shell 脚本的情况下运行 python 脚本。

这是到目前为止的脚本:

#!/bin/bash
tsv=/home/user/tsv
fasta=/home/user/fasta/*
clustering=/home/user/clustering

for file in ${fasta}
do
    python blastn_new.py --fasta ${file} --tsv ${tsv}/${file}.tsv &&
    mkdir ${clustering}/${file} &&
    mkdir ${clustering}/${file}/clusters &&
    python blastparsPB.py --clusters ${clustering}/${file}/${file}.txt --fish ${tsv}/${file}.tsv --dir ${clustering}/${file}/clusters/
done

最佳答案

您可以在后台运行第二个脚本。

以下内容也有一些无关紧要的注释,并稍微重新格式化您的代码。

#!/bin/bash

# You cannot have spaces around the equals signs
# Also, avoid hard-coding an absolute path
tsv=./tsv
db=./newpacbiodb/pacbiodb
clustering=./clustering

# Notice proper quoting throughout
for file in ./fasta/*
do
    python blastn_new.py \
        --fasta "${file}" \
        --tsv "${tsv}/${file}.tsv" &&
    # mkdir -p creates an entire path if necessary
    # (and works fine even if the directory already exists)
    mkdir -p "${clustering}/${file}/clusters" &&
    python blastparsPB.py \
        --clusters "${clustering}/${file}/${file}.txt" \
        --fish "${tsv}/${file}.tsv" \
        --dir "${clustering}/${file}/clusters/" &
done # notice the simple addition of background ^ job

显然,这假设第二个 Python 脚本不喜欢连接某些东西,例如同时写入数据库,但这已经是给定的了。

关于python - 使用多个命令运行 for 循环文件,当第一个命令前一个文件完成时运行下一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53224875/

相关文章:

python - 包含 100 个元素的列表,每个元素长 30

python - 从 python 脚本启动 vim 并结束脚本

android - 在 bash 脚本中运行 apktool

linux - 如何在 linux 中捕获 "sort -c"的输出

git - 将票号附加到本地和远程 git 历史记录中的所有提交消息

python 后期绑定(bind) - 动态地将局部变量放在范围内

Python简单 turtle 程序

bash - Bash 中管道如何工作的简单解释是什么?

regex - 在 bash 中连接两个没有换行符的命令的输出

c - 用于 wandboard 的 c GPIO 编程