开发者

Recursive logic in shell scripts

Here is a problem that i need solution for:

Say file A contains names of files B,C,D. And file B contains file names E,F,G etc. File C contains names of files H,I,J and so on......

I have to parse the files starting from A ,and copy the files mentioned in A to dir DIR. I wanna do the same parsing on all the child files B,C,D and get their child files into my dir DIR. This should go on until i reach the last file say Z which doesn't contain any other file names.

How do i do that?

I wanna do the whole thing in a single script and any further optimization would be appreciat开发者_StackOverflow中文版ed.

Thanks in advance.


If the files contain other data than file names more parsing could be necessary.

DIR="$HOME/DIR"
startfile="a"
counter=0

copy_to_dir ()
{
  while read line ; do
    if [ -f "$line"  ] ; then
      cp "$line" "$2" && ((counter++))
      copy_to_dir "$line" "$2"                  # recurse
    fi
  done < "$1"
} # ----------  end of function copy_to_dir  ----------

if [ -f "$startfile" -a -d "$DIR" ] ; then
  copy_to_dir "$startfile" "$DIR"                        # start copying
fi

printf "'%s' : %d files copied to '%s'\n" "$0" $counter "$DIR"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜