【问题标题】:bash script - for loopbash 脚本 - for 循环
【发布时间】:2021-03-18 01:34:59
【问题描述】:

假设我有一个 txt 文件,例如:

CGND_01234    CGND-HRA-00736
CGND_34563    CGND-HRA-00567
...

如何在 bash 中为下面的行创建一个 for 循环来迭代文件中的每一行?

find ./${first_word_in_the_first_line} -name "${second_word_in_the_first_line}*.bam" -type f 
find ./${first_word_in_the_second_line} -name "${second_word_in_the_second_line}*.bam" -type f
...

【问题讨论】:

    标签: bash for-loop find


    【解决方案1】:

    while read 循环是read a file line-by-line 的常用方法。方便的是,如果您将多个变量名称传递给read,它也会同时拆分行。

    while read -r dir file; do
        find "$dir" -name "$file*.bam" -type f 
    done < file.txt
    

    【讨论】:

    • 这可能应该使用read -r(re.shellcheck)和不同的FD(例如read -u9done 9&lt; file.txt),以防OP想要在里面使用stdin-swallowing命令循环。
    • 我添加了read -r。我将传递不同的 FD 想法;不想把事情复杂化。
    • 工作。谢谢@JohnKugelman 我还必须使用 fromdos 将 dos 转换为 UNIX,因为我的文本文件在 dos 中
    猜你喜欢
    • 1970-01-01
    • 2013-11-07
    • 1970-01-01
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多