I have a simple problem I want to solve with a bash script: copy a file, and also copy all the files that are imported in that file, and imported in that file, and so on. This screams recursion.
The files look like this:
import "/path/to/otherfile.txt"
import "/path/to/anotherfile.txt"
information
otherinformation
...
Shouldn't be so hard, here's what I wrote:
#!/bin/bash
destination=/tmp
copy_imports () {
insfile=$1
contained_imports=$(grep "import" $insfile | awk -F' ' '{ print $2 }' | sed 's/"//g')
for imported_insfile in $contained_imports
do
copy_imports $imported_insfile
done
cp $insfile $destination
}
copy_imports $1
But for some reason, not all files are copied. I see that it is calling the recursion for all the files and nested imports, but not for all of them the cp
statement is executed.
I'm totally puzzled, what's going on here?
Thanks a lot!