Bash bucles directorios

0

Mi tarea simple es crear dentro del directorio de trabajo hasta un máximo de direcciones separadas

  1. copie dentro de él algunos archivos del directorio de plantillas (de modo que cada nuevo directorio son réplicas y la diferencia solo dentro de los nombres definidos en i)
  2. cree un nuevo archivo txt dentro de cada uno de los nuevos directorios con algún texto:

    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
    

Desafortunadamente, los nuevos directorios se crean pero los archivos no se copiaron

¡Gracias por la ayuda!

    
pregunta user3470313 11.03.2016 - 15:44

1 respuesta

1

Hay varias cosas en tu script que pueden hacer que salga mal:

#!/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
    
respondido por el nohillside 11.03.2016 - 16:40

Lea otras preguntas en las etiquetas