【问题标题】:While loop error: syntax error near unexpected token `done'While循环错误:意外标记“完成”附近的语法错误
【发布时间】:2016-07-05 18:47:29
【问题描述】:

我是 bash 脚本的新手。我想逐行读取包含表格的文件。在每一行,我想将值分配给变量。这基本上是我的代码格式:

#!/bin/bash

while IFS= read -r line; do
    echo $a $b $c $d
    #if statements…
done < test.txt

我收到此错误:“While 循环错误:意外标记 `done' 附近的语法错误”

知道我的代码有什么问题吗?我尝试查看与同一错误相关的其他几个问题,但我无法修复我的代码。感谢您的帮助!

编辑:这是整个代码。它可能还有其他错误(我真的是初学者),但我希望它有所帮助。

#!/bin/bash

experiment_database="/home/gperron/datasets/ENCODE_tables/final_tables/test_experiment_database.txt"
file_database="/home/gperron/datasets/ENCODE_tables/final_tables/test_file_database.txt"

EXPERIMENT=`cat $experiment_database | head -n $PBS_ARRAYID | tail -n 1 | cut -f1`
echo "EXPERIMENT #$PBS_ARRAYID: $EXPERIMENT"

while IFS= read -r line; do
        echo $file_ID $experiment $target $biological_replicate $technical_replicate $read_number $url $fastq_file $genome
        if [ "$experiment" == "$EXPERIMENT" ]; then
                if [ $biological_replicate == 1 ]; then
                        if [ $technical_replicate == 1 ]; then
                                if [ $read_number == 1 ]; then
                                        ID_111= $file_ID
                                        fastq_111= $fastq_file
                                        url_111= $url
                                        echo "FASTQ file 111 ID: $ID_111"
                                        echo "FASTQ file 111: $fastq_111"
                                        echo "FASTQ file 111 url: $url_111"
                                elif [ $read_number == 2 ]; then
                                        ID_112= $file_ID
                                        fastq_112= $fastq_file
                                        url_112= $url
                                        echo "FASTQ file 112 ID: $ID_112"
                                        echo "FASTQ file 112: $fastq_112"
                                        echo "FASTQ file 112 url: $url_112"
                                fi
                        elif [ $technical_replicate == 2 ]; then
                                if [ $read_number == 1 ]; then
                                        ID_121= $file_ID
                                        fastq_121= $fastq_file
                                        url_121= $url
                                        echo "FASTQ file 121 ID: $ID_121"
                                        echo "FASTQ file 121: $fastq_121"
                                        echo "FASTQ file 121 url: $url_121"
                                elif [ $read_number == 2 ]; then
                                        ID_122= $file_ID
                                        fastq_122= $fastq_file
                                        url_122= $url
                                        echo "FASTQ file 122 ID: $ID_122"
                                        echo "FASTQ file 122: $fastq_122"
                                        echo "FASTQ file 122 url: $url_122"
                                fi
                elif [ $biological_replicate == 2 ]; then
                        if [ $technical_replicate == 1 ]; then
                                if [ $read_number == 1 ]; then
                                        ID_211= $file_ID
                                        fastq_211= $fastq_file
                                        url_211= $url
                                        echo "FASTQ file 211 ID: $ID_211"
                                        echo "FASTQ file 211: $fastq_211"
                                        echo "FASTQ file 211 url: $url_211"
                                elif [ $read_number == 2 ]; then
                                        ID_212= $file_ID
                                        fastq_212= $fastq_file
                                        url_212= $url
                                        echo "FASTQ file 212 ID: $ID_212"
                                        echo "FASTQ file 212: $fastq_212"
                                        echo "FASTQ file 212 url: $url_212"
                                fi
                        elif [ $technical_replicate == 2 ]; then
                                if [ $read_number == 1 ]; then
                                        ID_221= $file_ID
                                        fastq_221= $fastq_file
                                        url_221= $url
                                        echo "FASTQ file 221 ID: $ID_221"
                                        echo "FASTQ file 221: $fastq_221"
                                        echo "FASTQ file 221 url: $url_221"

                                elif [ $read_number == 2 ]; then
                                        ID_222= $file_ID
                                        fastq_222= $fastq_file
                                        url_222= $url
                                        echo "FASTQ file 222 ID: $ID_222"
                                        echo "FASTQ file 222: $fastq_222"
                                        echo "FASTQ file 222 url: $url_222"

                                fi
                        fi
                fi
        fi
done < test_file_database.txt

如果我有任何澄清,请告诉我。

【问题讨论】:

  • 发布循环的内容。你可能有一些未闭合的嵌套结构或不平衡的括号或其他东西。
  • 我在您发布的代码中没有发现任何错误。创建一个重现问题的最小示例,并将该确切代码复制并粘贴到您的问题中。 (您可能会在缩小测试用例范围的同时自己解决问题;在这种情况下,您可以删除问题或发布答案,具体取决于它是否对其他人有用。)
  • 您发布的代码显然可以按原样运行
  • 我编辑了我的帖子并添加了整个代码,也许它会有所帮助。我的其余代码可能会令人困惑,如果我能澄清任何事情,请告诉我!
  • 基思所说的:创建一个最小的例子。可能是通过删除块,并在每次删除块后检查错误是否仍然存在。 (此时我的假设是您没有正确计算您的 if/fi。

标签: bash syntax while-loop


【解决方案1】:

在接近顶部的$biological_replicate == 1 之后,if [ $technical_replicate == 1 ]; thenelif 之后没有fi。见下文。

我认为您可能对indirect expansion 感兴趣,如下所示。 (可能很笨拙;我自己最近才发现间接扩展。)

this_id="${biological_replicate}${technical_replicate}${read_number}"   # e.g., 111, 112, ...
declare "ID_${this_id}"=$file_id   # evaluates the left side into, e.g., ID_111, and sets $ID_111=$file_id

我认为这可以大大减少您的 if 链。


从上面编辑的代码:

while IFS= read -r line; do
    echo $file_ID $experiment $target $biological_replicate $technical_replicate $read_number $url $fastq_file $genome
    if [ "$experiment" == "$EXPERIMENT" ]; then
            if [ $biological_replicate == 1 ]; then
                    if [ $technical_replicate == 1 ]; then
                            if [ $read_number == 1 ]; then
                                    ID_111= $file_ID
                                    fastq_111= $fastq_file
                                    url_111= $url
                                    echo "FASTQ file 111 ID: $ID_111"
                                    echo "FASTQ file 111: $fastq_111"
                                    echo "FASTQ file 111 url: $url_111"
                            elif [ $read_number == 2 ]; then
                                    ID_112= $file_ID
                                    fastq_112= $fastq_file
                                    url_112= $url
                                    echo "FASTQ file 112 ID: $ID_112"
                                    echo "FASTQ file 112: $fastq_112"
                                    echo "FASTQ file 112 url: $url_112"
                            fi
                    elif [ $technical_replicate == 2 ]; then
                            if [ $read_number == 1 ]; then
                                    ID_121= $file_ID
                                    fastq_121= $fastq_file
                                    url_121= $url
                                    echo "FASTQ file 121 ID: $ID_121"
                                    echo "FASTQ file 121: $fastq_121"
                                    echo "FASTQ file 121 url: $url_121"
                            elif [ $read_number == 2 ]; then
                                    ID_122= $file_ID
                                    fastq_122= $fastq_file
                                    url_122= $url
                                    echo "FASTQ file 122 ID: $ID_122"
                                    echo "FASTQ file 122: $fastq_122"
                                    echo "FASTQ file 122 url: $url_122"
                            fi
                    fi   # <----- this one was missing
            elif [ $biological_replicate == 2 ]; then
            ...

【讨论】:

  • @arielle 很高兴为您提供帮助!欢迎访问该网站 - 不要忘记查看tour 以了解更多关于提出问题以吸引高质量答案的信息。另请参阅我添加到此答案的替代方案。
  • 感谢您的提示!我将研究间接扩展并使用我的代码进行尝试。
猜你喜欢
  • 2011-08-02
  • 1970-01-01
  • 2015-05-10
  • 2015-01-01
  • 2018-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多