2016-07-05 98 views
1

我是新來的bash腳本。我想要逐行讀取包含表格的文件。在每一行我都想把值賦給變量。這基本上是我的代碼的格式:雖然循環錯誤:語法錯誤附近的意外令牌`完成'

#!/bin/bash 

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

我得到這個錯誤:「雖然環錯誤:附近意外的標記語法錯誤'完成」」

任何想法是錯我的代碼?我試着查看與同一個錯誤有關的其他幾個問題,但我無法修復我的代碼。感謝您的幫助!

編輯:這是整個代碼。它可能有其他錯誤(我真的是一個初學者),但我希望它有幫助。

#!/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 

請讓我知道,如果我得到任何澄清。

+0

後循環的肉。你可能有一些未封閉的嵌套構造或不平衡的parens或東西。 –

+1

我在您發佈的代碼中看不到任何錯誤。創建一個再現問題的最小示例,並將* exact *代碼複製並粘貼到您的問題中。 (您可能會在縮小測試用例的同時自己解決問題;在這種情況下,您可以刪除問題或發佈答案,具體取決於其他用戶是否有用。) –

+0

您發佈的代碼顯然適用於-is –

回答

0

$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 
      ... 
+0

你是對的,非常感謝你! – arielle

+0

@arielle很高興幫助!歡迎來到本網站 - 不要忘記查看[tour](https://stackoverflow.com/tour)瞭解更多關於提問的問題。將吸引高質量的答案,另請參閱我添加到此答案的替代方案 – cxw

+1

感謝您的提示!我將研究間接擴展,並使用我的代碼進行試用。 – arielle

相關問題