问题
I have a script, which takes a list of directories and compares their number of files. What I would like is to sometimes group two or more directories to be treated as one by the script. Of course I could modify the script but I would prefer to do this grouping in the command line.
$ ./myscript.sh {dir1 dir2} dir3 dir4 ...
should be treated as if {dir1 dir2} was the (mathematical) union of the elements of both directories.
How to achieve this?
回答1:
You can use an array of directories for this purpose like so:
## declare an array variable
declare -a arr=("/home/x/dir1" "/home/x/dir2" "/home/x/dir3")
## now loop through the above array
for i in "${arr[@]}"
do
./myscript.sh $i dir3 dir4 ...
done
回答2:
How about merging the directory contents together in a separate script, and then passing that script as a parameter:
file1=$1
file2=$2
cp -R $file1/* $file2
echo $file2
Then you can use the script as follows:
./myscript "$(./merge dir1 dir2)" dir3 ...
来源:https://stackoverflow.com/questions/27768095/how-to-treat-2-directories-as-one-in-the-command-line