简化并并行 bash if 语句脚本

简化并并行 bash if 语句脚本

我写了一个脚本,在目录中查找文件,并通过 if 语句引入,这里是代码:

for dirname in /input/*; do

    id=${dirname#/input/}    # remove "/input/sub-"
    id=${id%/}             # remove trailing "/"

    printf 'Adding ID to recon-all processing list: %s\n' "${id}" >&2

    T11=`find /input/${id}/unprocessed/3T -name "*T1*MPR1*" -type f`
    T12=`find /input/${id}/unprocessed/3T -name "*T1*MPR2*" -type f`

    T21=`find /input/${id}/unprocessed/3T -name "*T2*SPC1*" -type f`
    T22=`find /input/${id}/unprocessed/3T -name "*T2*SPC2*" -type f`

    if [ -z "$T11" ] || [ -z "$T12" ] || [ -z "$T21" ] || [ -z "$T22" ]; then
        recon-all -s "${id}" -i "${T11}" -i "${T12}" -i "${T21}" -i "${T22}"
    elif [ -z "$T11" ] || [ -z "$T12" ] || [ -z "$T21" ]; then
        recon-all -s "${id}" -i "${T11}" -i "${T12}" -i "${T21}"
    elif [ -z "$T11" ] || [ -z "$T12" ] || [ -z "$T22" ]; then
        recon-all -s "${id}" -i "${T11}" -i "${T12}" -i "${T22}"
    elif [ -z "$T11" ] || [ -z "$T21" ]; then
        recon-all -s "${id}" -i "${T11}" -i "${T21}"
    elif [ -z "$T11" ] || [ -z "$T22" ]; then
        recon-all -s "${id}" -i "${T11}" -i "${T22}"
    else 
        recon-all -s "${id}" -i "${T11}" -i "${T21}" 
    fi

    if [ -e "/output/$subj_id" ]; then
        # no output file corresponding to this ID found,
        # add it to he list
        all_ids+=( "$subj_id" )
    fi
done

问题是目录内可能有不同的组合,有时可能会错过 T12 和 T22,这就是为什么我recon-all为每个语句都做了一个。如何简化 if 语句并并行此脚本?

答案1

我想知道你是否想打电话recon-all所有非空变量。如果是这种情况,您可能需要这样:

opts=( -s "$id" )
for val in "$T11" "$T12" "$T21" "$T22"; do
    [[ -n "$val" ]] && opts+=( -i "$val" )
done
recon-all "${opts[@]}"

相关内容