shell 为什么我关闭for循环时代码不理解?

lrpiutwd  于 2023-01-21  发布在  Shell
关注(0)|答案(1)|浏览(148)

我正在用Shell脚本编写一段代码,试图使数据处理中的一些过程自动化,但我遇到了一些关于for的问题。代码如下所示。看起来他明白我已经关闭了for循环。

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

代码的名称为RFauto,当执行以下操作时:
bash ./RF
我得到的信息是:www.example.com:第79行:意外标记done' RFauto.sh: row 79:旁边的语法错误完成'RFauto.sh: row 79: syntax's error next to the unexpected token done' RFauto.sh: row 79: done'
(我不得不把错误消息从葡萄牙语翻译过来,但我希望它是可以理解的)
就像我说的,他是在说我已经关闭了for循环,但是当我删除它的时候,它说我需要关闭for循环。
如有任何帮助,我将不胜感激!

jpfvwuh4

jpfvwuh41#

1.你需要for i in "${models[@]}"来迭代数组的元素。
1.这是正确的shell语法,但不是您想要的

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

这将尝试在子shell中执行命令-e,并返回否定的退出状态。未找到命令””。
您需要使用[[...]]条件构造
此外,mkdir -p将创建一个目录,如果它不存在(包括父目录,如果它们不存在);如果它确实存在,则没有错误
1.

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

cd命令更改了$PWD。您确定这是您想要的吗?通常在程序中使用$PWD是有风险的:当用户调用脚本时,您真的知道用户的位置吗?

  1. ijk不是非常具有描述性的变量名。
    1.临时文件通常是不必要的。
#!/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

相关问题