How to treat 2 Directories as one in the command line

落花浮王杯 提交于 2019-12-11 20:17:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!