1

I am new to bash scripting. I want to read a file containing a table line by line. At each line I want to assign the values to variables. This is basically the format of my code:

#!/bin/bash

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

I get this error: "While loop error: syntax error near unexpected token `done' "

Any idea what is wrong with my code? I have tried looking at several other questions related to the same error, but I was not able to fix my code. Thank you for your help!

EDIT: This is the whole code. It might have other errors in it (I am really a beginner), but I hope it helps.

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

Please let me know if I get clarify anything.

arielle
  • 915
  • 1
  • 12
  • 29
  • Post the meat of the loop. You probably have some unclosed nested construct or unbalanced parens or something. – Mad Physicist Jul 05 '16 at 18:53
  • 1
    I see no errors in the code you posted. Create a minimal example that reproduces the problem, and copy-and-paste that *exact* code into your question. (It's likely you'll solve the problem yourself while narrowing down the test case; in that case you can either delete the question or post an answer, depending on whether it would be useful to others.) – Keith Thompson Jul 05 '16 at 18:54
  • The code you posted clearly works as-is – Mad Physicist Jul 05 '16 at 18:54
  • I edited my post and added the whole code, maybe it will help. The rest of my code might be confusing, please let me know if I can clarify anything! – arielle Jul 05 '16 at 19:02
  • What Keith said: Create a minimal example. Probably by deleting blocks and, after each block deletion, checking that the error is still there. (My assumption at this point would be that you did not count you if/fi correctly. – Christopher Creutzig Jul 05 '16 at 19:07
  • Alright, I'll try deleting blocks of codes and I'll post the answer if I find it – arielle Jul 05 '16 at 19:10
  • Run your code through shellcheck.net. – chepner Jul 05 '16 at 19:19

1 Answers1

0

After $biological_replicate == 1 close to the top, if [ $technical_replicate == 1 ]; then doesn't have a fi after the elif. See below.

I think you may be interested in indirect expansion, something like the below. (Probably clumsy; I've only recently discovered indirect expansion myself.)

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

I think this could cut down on your if chains quite a bit.


Edited code from above:

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
            ...
Community
  • 1
  • 1
cxw
  • 16,685
  • 2
  • 45
  • 81
  • @arielle Glad to help! Welcome to the site - don't forget to check out the [tour](https://stackoverflow.com/tour) for more about asking questions that will attract quality answers. See also the alternative I added to this answer. – cxw Jul 05 '16 at 19:20
  • 1
    Thank you for the tip! I will look into indirect expansion and try it with my code. – arielle Jul 05 '16 at 19:29