#!/bin/sh
param1=$1
param2=$2
recursive(){
mkdir -p $2
cd $1
for file in `ls $1`; do
[ $file = "." -o $file = ".." ] && continue
[ -d $file ] && recursive $1"/"$file $2"/"$file
[ -f $file ] && ln -s $1"/"$file $2"/"$file
done
}
recursive $param1 $param2
If I execute this script, that it call self (recursive). Why not scan all directories?
(excuse me: my english is poor)
Don’t call
lsin backquotes when wildcard will do and is more reliable, that is:The variable expansions have to be inside the quotes or it will all fall down on any special character including space. You should use
"$1","$2"and"$1/$file" "$2/$file".Variables are global in shell by default. So the recursive call clobbers the outer call’s
filevariable. There are two possible workarounds:Declare the variable local with
at the beginning of the function. This is “bashishm”, i.e. it’s not defined by POSIX shell standard, so some shells don’t have it.
Wrap the function in round parenthesis instead of curly brackets. That will make the function run in a subshell, which can’t clobber it’s parents’s variables.
Oh, you don’t need the
param1andparam2. The positional parameters are scoped.William Pusell (see the other answer) notices one more thing. Either cd into the argument directory or prefix it to the path, but don’t do both.
With all the fixes you should get to:
I didn’t test it, so there may still be some problem left.