0

My simple task is to create within the workdir up to max separate dirs

  1. copy within it some files from the template dir (so every new dir is replicas and the difference only within it names defined from i)
  2. create new txt file within each of the new dir with some text:

    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
    

Unfortunately the new dirs are created but files didn't copy

Thanks for the help!

1

There are several things in your script which can make it go wrong:

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .