bash - 在 bash 中使用变量参数和输入文件名命名输出文件

标签 bash parameters output filenames

我在子目录中有一系列文件,我想根据输入文件名和我用来处理文件的各种参数(模型)循环、处理和命名这些文件。

例如,像 AG005574、AG004788、AG003854 这样的文件名和像 ATd、PZa、RTK1 这样的参数/模型值,所以我想以这样的文件结尾 AG005574_ATd AG005574_PZa AG005574_RTK1 AG004788_ATd AG004788_PZa ETC。 我循环遍历子文件夹,运行该过程并输出结果,如下所示:

#!/usr/bin/bash

model=$1
for file in $(find /path/to/files/*/ -type f -name 'AG*.fa');
     do output=${model}"_"${file} ;
        process_call --out=$output."tab" --options ../Path/to/model/$1.hmm $file ;
    echo $file
done

我希望能够在命令行上指定模型(因此 model=$1)。然而,我的方法并不适用于一般情况;我可以使用

获取按模型命名的输出
do output=$model ;

但这也只写入最后处理的文件,因为它会覆盖所有其他文件(并且不使用输入文件名)。非常感谢任何帮助/辅导。

最佳答案

将所有模型名称作为参数传递给脚本:

/path/to/script ATd PZa RTK1

然后

#!/bin/bash    
find /path/to/files/*/ -type f -name 'AG*.fa' | 
while IFS= read -r file; do
    echo "$file"
    for model in "$@"; do
        output="${file%.fa}_$model.tab"
        process_call --out="$output" --options "../Path/to/model/$model.hmm" "$file"
    done
done

如果您已经了解所有模型,则可以将其构建到脚本中:

#!/bin/bash    
models=( ATd PZa RTK1 )
...
    for model in "${models[@]}"; do
...

关于bash - 在 bash 中使用变量参数和输入文件名命名输出文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27947025/

相关文章:

bash - Shell 命令 SET -X 在脚本中的意思

bash 打印转义文件内容

运行 MATLAB 的 Bash 脚本错误

linux - 比 wc -l 更快更精确地计算行数的方法

r - 修改 SIR 模型以包含随机性

android - Eclipse 内容辅助问题

使用 ThreadPool 的 C# Execute 方法(带参数)

php - 使用php检测并输出特定程序是否正在运行

windows - Stack Overflow 答案中 2>nul 的用途是什么?

javascript - 输入越低,输出越高