Bash directory di loop

0

Il mio compito semplice è quello di creare all'interno del workdir fino a max dirs separati

  1. copia al suo interno alcuni file dalla directory del modello (quindi ogni nuova dir è repliche e la differenza solo all'interno dei nomi definiti da i)
  2. crea un nuovo file txt all'interno di ciascuna nuova directory con del testo:

    home=/projects/clouddyn/md_bench/for_server/MD_bench
    template=$home/template
    
    cd $home
    min=1
    max=3
    
    for i in $(eval echo "{$min..$max}")
    do
        mkdir "sim_$i"
        sim=$(basename "sim_$i")
        pushd $sim
        cp $template/*.* $sim
        prinf "This is $i dir" > ./$sim/file_$i.txt
        popd
    done
    

Sfortunatamente le nuove cartelle vengono create ma i file non sono stati copiati

Grazie per l'aiuto!

    
posta user3470313 11.03.2016 - 15:44
fonte

1 risposta

1

Ci sono diverse cose nel tuo script che possono farlo andare storto:

#!/bin/bash
home=/projects/clouddyn/md_bench/for_server/MD_bench

# home is a bad choice, because $HOME has a special meaning and it's
# confusing to have both $HOME and $home. But it doesn't make the script fail

template=$home/template

cd $home
min=1
max=3

# for i in $(eval echo "{$min..$max}")
# this can be replaced by the easier to read
for i in {$min..$max}
do
    mkdir "sim_$i"

    # sim=$(basename "sim_$i")
    # basename strips the path part, but "sim_$i" doesn't even have a path
    # so basically this just assigned "sim_$i" to $sim and can be replaced by
    sim="sim_$i"
    # Ideally this assignment would be the first statement after 'do' so 
    # it could be used already in 'mkdir'

    # pushd $sim
    # cp $template/*.* $sim
    # Two problems here:
    # - 'pushd' already changes into $sim, so when 'cp' is called there is
    #   no $sim directory to copy into (as you are already in it)
    # - '$template/*.*' only matches files with a dot in the name, which may
    #   or may not be what you want
    # As there is no need to 'cd' into a directory to copy files into it I
    # would just use
    cp $template/* $sim/

    # prinf "This is $i dir" > ./$sim/file_$i.txt
    # You probably meant 'printf' here. Also this line has the same problem
    # as the 'cp' above (you are already in $sim)
    printf "This is $i dir" > $sim/file_$i.txt

    # popd
    # Not needed any longer
done
    
risposta data 11.03.2016 - 16:40
fonte

Leggi altre domande sui tag