1

What does this part of a script do? This is a part of a scrpt in bash

for j in *.* ; do 
    cp $j ../../$name-S$i.gid/data/${j%%.*}$i.${j#*.}
    sed "s/$name-S/$name-S$i/" $j > ../../$name-S$i.gid/data/${j%%.*}$i.${j#*.}
done

I forgot to say that "i", is a parameter that goes from 1 to specific number and "$name" is a part of name of a folder.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
carlo
  • 21
  • 2
  • 1
    Refer to _Shell Parameter Expansion_ in your shell reference manual. – devnull May 20 '14 at 16:38
  • 1
    What language is that written in, can we assume bash? Also we don't know what it does because you left out the part that defines `$i`. Not to mention `$name`. – terdon May 20 '14 at 17:17

1 Answers1

1

This is stupid code. First I rewrite it so that this becomes obvious (I add quoting, too):

for j in *.* ; do 
    target_file="../../$name-S$i.gid/data/${j%%.*}$i.${j#*.}"
    cp "$j" "$target_file"
    sed "s/$name-S/$name-S$i/" "$j" >"$target_file"
done

I.e. a file is copied and immediately afterwards the new file is overwritten. This is done for all files whose name contains a dot (but probably not at the beginning; depends (in bash) on the setting of dotglob).

The target file path is constructed as:

  1. Put it in another directory.

  2. Erase the file extension (all parts of it, i.e. everything from the first dot).

  3. Add the number i and then the old extension.

The sed call replaces only the first (intentional limitation?) occurrance of $name-S (i.e. its expansion) in a line by $name-S$i (its expansion again).

Hauke Laging
  • 88,146
  • 18
  • 125
  • 174