Felipe Corral
Felipe Corral

Reputation: 5

Why my code don't understand when I close the for loop?

I am writing a code in Shell Script trying to automatize some procedures in my data processing, but I facing some problems concerning the for. The code is the following. And it seems like he is understanding that I already closed the for loop.

#!/bin/bash

#modelos que serão usados
models=( 410_5x5_nocrust ) # 660_5x5_nocrust mtz_2x2_nocrust mtz_5x5_nocrust mtz_8x8_nocrust mtz_11x11_nocrust prem_nocrust prem s40rts_nocrust 660_5x5_s40rts_nocrust tx2015_nocrust)

#pasta base de onde todos os arquivos e programas estão
home_folder=/home/felipe/sda1_acess/RF_PROCESSING


for i in $models
do 
    
    #criando as pastas importantes para o trabalho
    if ( ! -e $PWD/Listas ) 
    then
            mkdir $PWD/Listas
    fi

    if ( ! -e $PWD/Listas/"$i"_lists ) 
    then
        mkdir $PWD/Listas/"$i"_lists
    fi

    if ( ! -e $PWD/sacev ) 
    then
            mkdir $PWD/sacev
    if

    if ( ! -e $PWD/sacev/sacev_$i ) 
    then
            mkdir $PWD/sacev/sacev_$i/
    fi

    #setando a pasta onde os arquivos estão
    folder=XH_preprocessed_"$i"_stations_1x1_RF_8s
    
    #acessando a pasta
    cd $home_folder/$folder

    #numero de eventos q temos por modelo
    v=(01 02) #03 04 05 06 07 08 09 10 11 12)   


    #Fazendo as listas
    for j in $(v)
    do
        list=lista_"$i"_"$j".dat
        xh_header "$i"_stations_1x1_RF_8s_01"$j"00X.cut.xh -h evnt stat arcs | sort -n -k25 > teste.dat
        cat teste.dat | awk '{print $4" "$5" "$6" "$10" "$12" "$13" "$17" "$19" "$25}' | sort -n -k9 > $list
        rm teste.dat

        for k in $list
        do 
            xh_2sac $PWD/$folder/"$i"_stations_1x1_RF_8s_01"$j"00X.cut.xh -s $k
            touch log.dat
            echo "r *$k*" > lixo1
            echo "chnhdr KT0 'P'" >> lixo1
            echo "chnhdr KT1 'Pdiff'" >> lixo1
            echo "chnhdr KT2 'S'" >> lixo1
            echo "chnhdr KT3 'Sdiff'" >> lixo1
            echo "chnhdr KT4 'PcP'" >> lixo1
            echo "chnhdr KT5 'PP'" >> lixo1
            echo "chnhdr KT6 'SS'" >> lixo1
            echo "chnhdr KT7 'P400s'" >> lixo1
            echo "chnhdr KT8 'P670s'" >> lixo1
            echo "w over" >> lixo1
            echo "quit" >> lixo1
            sac < lixo1
            rm lixo1
        done

    done

    cd $home_folder
    mv $home_folder/$folder/lista* $home_folder/Listas/"$i"_lists/
    mv $PWD/$folder/*.sac $PWD/sacev/sacev_$i/
    #mv $PWD/$folder/log.dat $PWD/sacev/sacev_$i/
    mv $PWD/sacev/sacev_$i/ $folder_path
done

the code's name is RFauto and when a do:

bash ./RF

I get the message: RFauto.sh: row 79: syntax's error next to the unexpected token done' RFauto.sh: row 79: done'

(I had to translate the error massage from portuguese, but I hope it is understandable)

Like I said, he is kinda saying that I already closed the for loop, but when I remove it, it says that I need to close the for loop.

I will be vary grateful for any help!

Upvotes: 0

Views: 65

Answers (1)

glenn jackman
glenn jackman

Reputation: 246774

  1. you need for i in "${models[@]}" to iterate over the elements of the array.

  2. This is correct shell syntax but not what you intend

        if ( ! -e $PWD/Listas ) 
        then
                mkdir $PWD/Listas
        fi
    

    That will attempt to execute the command -e in a subshell and return the exit status negated. I would expect to see the error "-e: command not found".

    You want to use the [[...]] conditional construct

    Additionally, mkdir -p will create a directory if it does not exist (including parent directories if they do not exist); if it does exist there is no error

  3. cd $home_folder
    mv $home_folder/$folder/lista* $home_folder/Listas/"$i"_lists/
    mv $PWD/$folder/*.sac $PWD/sacev/sacev_$i/
    #mv $PWD/$folder/log.dat $PWD/sacev/sacev_$i/
    mv $PWD/sacev/sacev_$i/ $folder_path
    

    The cd command changed $PWD. Are you sure that's what you want? In general using $PWD in a program is risky: do you really know the user's location when they invoke the script?

  4. i j k are not very descriptive variable names.

  5. temporary files are usually not necessary.

#!/bin/bash

#modelos que serão usados
models=( 410_5x5_nocrust ) # 660_5x5_nocrust mtz_2x2_nocrust mtz_5x5_nocrust mtz_8x8_nocrust mtz_11x11_nocrust prem_nocrust prem s40rts_nocrust 660_5x5_s40rts_nocrust tx2015_nocrust)

#numero de eventos q temos por modelo
v=(01 02) #03 04 05 06 07 08 09 10 11 12)   

#pasta base de onde todos os arquivos e programas estão
home_folder=/home/felipe/sda1_acess/RF_PROCESSING

for i in "${models[@]}"
do 
    
    #criando as pastas importantes para o trabalho
    mkdir -p "$PWD/Listas/${i}_lists"
    mkdir -p "$PWD/sacev/sacev_${i}/"

    #setando a pasta onde os arquivos estão
    folder="XH_preprocessed_${i}_stations_1x1_RF_8s"
    
    #acessando a pasta
    cd "$home_folder/$folder"

    #Fazendo as listas
    for j in "${v[@]}"
    do
        list="lista_${i}_${j}.dat"
        xh_header "${i}_stations_1x1_RF_8s_01${j}00X.cut.xh" -h evnt stat arcs \
        | sort -n -k25 \
        | awk '{print $4, $5, $6, $10, $12, $13, $17, $19, $25}' \
        | sort -n -k9 \
        | while IFS= read -r k
          do 
              xh_2sac "$PWD/$folder/${i}_stations_1x1_RF_8s_01${j}00X.cut.xh" -s "$k"
              touch log.dat
              sac << END_INPUT
r *$k*
chnhdr KT0 'P'
chnhdr KT1 'Pdiff'
chnhdr KT2 'S'
chnhdr KT3 'Sdiff'
chnhdr KT4 'PcP'
chnhdr KT5 'PP'
chnhdr KT6 'SS'
chnhdr KT7 'P400s'
chnhdr KT8 'P670s'
w over
quit
END_INPUT
          done

    done

    cd "$home_folder"
    mv "$home_folder/$folder/lista"* "$home_folder/Listas/${i}_lists/"
    mv "$PWD/$folder/"*.sac "$PWD/sacev/sacev_${i}/"
    #mv "$PWD/$folder/log.dat" "$PWD/sacev/sacev_${i}/"
    mv "$PWD/sacev/sacev_${i}/" "$folder_path"
done

Upvotes: 1

Related Questions