首页 > 解决方案 > 带有 cat EOF 问题的嵌套 for 循环

问题描述

这是我的代码,我有两个文件,其中一个包含文件位置,另一个包含输入名称。我想创建一个用于合并这两个文件的 for 循环。我在这里试过

#!/bin/bash -l
x=`cat x_file_locations`
y=`cat y_list`
for i in $x
do
    for j in $y
    do
            cat << EOF >> ./algo/job_$j.sh
            #!/bin/bash -l
            #
            #$ -N $i
            #$ -cwd
            #$ -pe smp 10
            #$ -l mem_free=20G

            algo --input_se $i --output $j --threads=10
            EOF
    done
done

我收到这个错误

line 24: warning: here-document at line 10 delimited by end-of-file (wanted `EOF')
line 25: syntax error: unexpected end of file

我究竟做错了什么?

标签: linuxbashfor-loop

解决方案


当你这样做时<< EOF,整行必须EOF没有空格,也不能制表符,不能在EOF字符串之前或之后。

做:

            cat << EOF >> ./algo/job_$j.sh
            #!/bin/bash -l
            #
            #$ -N $i
            #$ -cwd
            #$ -pe smp 10
            #$ -l mem_free=20G

            algo --input_se $i --output $j --threads=10
EOF
   ^ no spaces after
^ no spaces in front 

或者,您可以使用<< -EOF前导选项卡(并且仅限选项卡!)将被忽略。

笔记:

在我的情况下,我将如何使用 while read 来逐行读取文件?

while IFS= read -r x; do
    while IFS= read -r y; do
        something something
    done < y_list
done < x_file_locations

推荐阅读